Html 如何使用 <a href="..."> 标签提交 POST 表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8169027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:33:00  来源:igfitidea点击:

How can I submit a POST form using the <a href="..."> tag?

htmlformsjsp

提问by hozaifa

How can I submit a POST form to showMessage.jspusing just the <a href="...">tag?

如何showMessage.jsp仅使用<a href="...">标签提交 POST 表单?

<form action="showMessage.jsp" method="post">
    <a href="showMessage.jsp"><%=n%></a>
    <input type="hidden" name="mess" value=<%=n%>/>
</form>

回答by rybo111

No JavaScript needed if you use a button instead:

如果您使用按钮,则不需要 JavaScript:

<form action="your_url" method="post">
    <button type="submit" name="your_name" value="your_value" class="btn-link">Go</button>
</form>

You can style a button to look like a link, for example:

您可以将按钮的样式设置为看起来像链接,例如:

.btn-link {
    border: none;
    outline: none;
    background: none;
    cursor: pointer;
    color: #0000EE;
    padding: 0;
    text-decoration: underline;
    font-family: inherit;
    font-size: inherit;
}

回答by hozaifa

You have to use Javascript submitfunction on your formobject. Take a look in other functions.

您必须submitform对象上使用 Javascript函数。看看其他功能

<form action="showMessage.jsp" method="post">
    <a href="javascript:;" onclick="parentNode.submit();"><%=n%></a>
    <input type="hidden" name="mess" value=<%=n%>/>
</form>

回答by naveed

You need to use javascript for this.

为此,您需要使用 javascript。

<form id="form1" action="showMessage.jsp" method="post">
    <a href="javascript:;" onclick="document.getElementById('form1').submit();"><%=n%></a>
    <input type="hidden" name="mess" value=<%=n%>/>
</form>

回答by Alexey Shevelyov

In case you use MVC to accomplish it - you will have to do something like this

如果你使用 MVC 来完成它 - 你将不得不做这样的事情

 <form action="/ControllerName/ActionName" method="post">
        <a href="javascript:;" onclick="parentNode.submit();"><%=n%></a>
        <input type="hidden" name="mess" value=<%=n%>/>
    </form>

I just went through some examples here and did not see the MVC one figured it won't hurt to post it.

我只是在这里浏览了一些示例,但没有看到 MVC 认为发布它不会有什么坏处。

Then on your Action in the Controller I would just put <HTTPPost>On the top of it. I believe if you don't have <HTTPGET>on the top of it it would still work but explicitly putting it there feels a bit safer.

然后在控制器中的 Action 上,我将放在<HTTPPost>它的顶部。我相信如果你没有<HTTPGET>在它上面它仍然可以工作但明确地把它放在那里感觉有点安全。

回答by Laid

There really seems no way for fooling the <a href= ..into a POST method. However, given that you have access to CSS of a page, this can be substituted by using a form instead.

似乎真的没有办法欺骗<a href= ..POST 方法。但是,鉴于您可以访问页面的 CSS,这可以通过使用表单来代替。

Unfortunately, the obvious way of just styling the button in CSS as an anchor tag, is not cross-browser compatible, since different browsers treat <button value= ...differently.

不幸的是,在 CSS 中将按钮样式化为锚标记的明显方法是不跨浏览器兼容的,因为不同的浏览器处理方式<button value= ...不同。

Incorrect:

<form action='actbusy.php' method='post'>
  <button type='submit' name='parameter' value='One'>Two</button>
</form>

The above example will be showing 'Two' and transmit 'parameter:One' in FireFox, while it will show 'One' and transmit also 'parameter:One' in IE8.

上面的例子将在 FireFox 中显示 'Two' 并传输 'parameter:One',而在 IE8 中将显示 'One' 并传输 'parameter:One'。

The way around is to use hidden input field(s) for delivering data and the button just for submitting it.

解决方法是使用隐藏的输入字段来传递数据,使用按钮来提交数据。

<form action='actbusy.php' method='post'>
   <input class=hidden name='parameter' value='blaah'>
   <button type='submit' name='delete' value='Delete'>Delete</button>
</form>

Note, that this method has a side effect that besides 'parameter:blaah' it will also deliver 'delete:Delete' as surplus parameters in POST.

请注意,此方法有一个副作用,除了 'parameter:blaah' 之外,它还会将 'delete:Delete' 作为 POST 中的多余参数传递。

You want to keep for a button the value attribute and button label between tags both the same ('Delete' on this case), since (as stated above) some browsers will display one and some display another as a button label.

您希望为按钮保留相同的标签之间的值属性和按钮标签(在这种情况下为“删除”),因为(如上所述)某些浏览器将显示一个,而有些浏览器将另一个显示为按钮标签。