javascript 如何使用 <a> 标签发布到表单并传递隐藏值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18530185/
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
How to post to a form using <a> tag and passing hidden value?
提问by Artisan
If I had some coding as follow.
如果我有一些编码如下。
<form method="POST" action="localhost/carts/delete">
<a href="#" onclick="$(this).closest('form').submit();">Item 1</a>
<a href="#" onclick="$(this).closest('form').submit();">Item 2</a>
<a href="#" onclick="$(this).closest('form').submit();">Item 3</a>
</form>
And I like to post to a form with some hidden value to indicate which Item is clicked, how to do that???
我喜欢张贴到一个带有一些隐藏值的表单来指示点击了哪个项目,怎么做???
Thanks.
谢谢。
Edited Text.
编辑的文本。
Thanks for so many useful suggestions.
感谢您提供这么多有用的建议。
I actually use Laravel 4 rather than PHP, my Laravel code produced this HTML
我实际上使用 Laravel 4 而不是 PHP,我的 Laravel 代码生成了这个 HTML
<form ...>
<ul>
<li>
<h1>Key: 1 </h1>
<input name="cart_id" type="hidden" value="1"><a href="#" onclick="$(this).closest('form').submit()">Delete</a>
</li>
<li>
<h1>Key: 2 </h1>
<input name="cart_id" type="hidden" value="2"><a href="#" onclick="$(this).closest('form').submit()">Delete</a>
</li>
<li>
<h1>Key: 6 </h1>
<input name="cart_id" type="hidden" value="6"><a href="#" onclick="$(this).closest('form').submit()">Delete</a>
</li>
<li>
<h1>Key: 7 </h1>
<input name="cart_id" type="hidden" value="7"><a href="#" onclick="$(this).closest('form').submit()">Delete</a>
</li>
</ul>
</form>
So, when I clicked on any items, I got 7 which is the last value of cart_id, how to place at the right place, in JavaScript???
所以,当我点击任何项目时,我得到了 7,这是cart_id 的最后一个值,如何在 JavaScript 中放置在正确的位置???
采纳答案by Alvaro
Create a hidden element inside the form:
在表单内创建一个隐藏元素:
<form method="POST" action="localhost/carts/delete">
<input type="hidden" name="myValue" value="" id="myValue"/>
<a href="#" data-value="1">Item 1</a>
<a href="#" data-value="2">Item 2</a>
<a href="#" data-value="3">Item 3</a>
</form>
Then on click over any link (a
) change its value and submit the form.
然后单击任何链接 ( a
) 更改其值并提交表单。
$('a').click(function(e){
//preventing the default link redirection
e.preventDefault();
$('#myValue').val($(this).data('value'));
$(this).closest('form').submit();
});
回答by Thijs Limmen
Use a hidden input field like:
使用隐藏的输入字段,如:
<input type="hidden" value="hidden value" name="id"/>
This box is not visible in your page.
此框在您的页面中不可见。
回答by Praxis Ashelin
Use input fields instead.
请改用输入字段。
<form method="POST" action="localhost/carts/delete">
<input type="submit" value="Item 1" name="whichitem[]" />
<input type="submit" value="Item 2" name="whichitem[]" />
<input type="submit" value="Item 2" name="whichitem[]" />
</form>
Then in PHP you can retrieve the clicked value like this:
然后在 PHP 中,您可以像这样检索点击的值:
$_POST["whichitem"]
If you are worried about the styling, simply add it in your css:
如果您担心样式,只需将其添加到您的 css 中:
input[type="submit"]{
//style
}