Javascript 如何使用锚标记通过 jquery 提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4674625/
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 can I use an anchor tag to submit a form with jquery
提问by coder
I want to use the following anchor to submit a form with jquery to Spring. How is this done?
我想使用以下锚点向 Spring 提交带有 jquery 的表单。这是怎么做的?
<a target="" title="" class="" href="">Save</a>
I've tried this, where requestNew is my form:
我试过这个,其中 requestNew 是我的表格:
$(document).ready(function(){
$("a").click(function(){
$("#requestNew").submit(function(){
$.post("../acctRequests", $("#requestNew").serialize());
});
});
});
It doesn't seem to go anywhere.
它似乎哪儿也去不了。
回答by lonesomeday
You are adding a new event handler; all you need to do is trigger the existing ones, and the browser's native functionality:
您正在添加一个新的事件处理程序;您需要做的就是触发现有的,以及浏览器的本机功能:
$(document).ready(function(){
$("a").click(function(){
$("#requestNew").submit();
});
});
回答by fatih.abdullah
Please use this
请使用这个
<a href="javascript:document.YOUR_FORM_NAME.submit()"> Submit</a>
<form name="YOUR_FORM_NAME" action="YOUR_ACTION_PAGE_URL" method=POST>
<input type="text" name="YOUR_INPUT_NAME" value="YOUR_INPUT_VALUE"/>
</form>
Replace "YOUR_ACTION_PAGE_URL" with your targeted action url
将“YOUR_ACTION_PAGE_URL”替换为您的目标操作网址
回答by Oundless
HTML
HTML
<form...>
...
<a data-role="submit">Save</a>
</form>
jQuery
jQuery
$(function(){
$("[data-role=submit]").click(function(){
$(this).closest("form").submit();
});
});
回答by user113716
Assign the submit handler outside the click. Then call it from the click.
在点击之外分配提交处理程序。然后从点击中调用它。
$(document).ready(function(){
// Binds the submit handler to the #requestNew form
$("#requestNew").submit(function(){
$.post("../acctRequests", $("#requestNew").serialize());
});
$("a").click(function(e) {
$("#requestNew").submit(); // calls the submit handler
e.preventDefault(); // Prevents the default behavior of the link
});
});
回答by wajiw
if you add an id to your anchor you can submit the form in your click function:
如果您向锚点添加 id,则可以在单击功能中提交表单:
<a target="" title="" class="" href="" id="saveButton">Save</a>
$(document).ready(function(){
$('#saveButton').click(function(){
$("#requestNew").submit(); //if requestNew is the id of your form
});
});
If you are trying to submit it with ajax that's a different solution but I'm not sure if that's what you want based on your question
如果您尝试使用 ajax 提交它,这是一个不同的解决方案,但根据您的问题,我不确定这是否是您想要的
回答by Bakh Domalak
the simplest way is this:
最简单的方法是这样的:
<a href="#" onclick="submit()">Submit</a>