jQuery 如果使用jquery在一个页面中存在多个表单,如何提交特定表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15129916/
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 submit a specific form if multiple forms are present in one page using jquery
提问by Gaurav123
I have two forms.
我有两种形式。
<form name="frm1" action="someurl" method="post">
<input type="submit" name="btn1" class="buttons" value="Submit"/>
</form>
and
和
<form name="frm2">
<input type="submit" name="btn2" value="Submit"/>
</form>
I need to submit form "frm1" on click of "btn2" of form "frm2".
我需要在单击表单“frm2”的“btn2”时提交表单“frm1”。
回答by katsarov
<button type="submit" form="form1" value="Submit">Submit</button>
The form
attribute specifies the id
of the form that the button will submit.
该form
属性指定id
按钮将提交的表单。
回答by Raab
you would say
你会说
<input type="submit" name="btn1" id="btn1" value="Submit"/>
<input type="submit" name="btn1" id="btn1" value="Submit"/>
$("#btn1").click(function(){
$("#frm1").submit();
}
and
和
<input type="submit" name="btn2" id="btn2" value="Submit"/>
<input type="submit" name="btn2" id="btn2" value="Submit"/>
$("#btn2").click(function(){
$("#frm1").submit();
}
回答by Shahbaz
HTML
HTML
<form name="frm1" action="someurl" method="post" id="frm1">
<input type="submit" name="btn1" class="buttons" value="Submit"/>
</form>
<input type="submit" name="btn2" onclick="formSubmit()" value="Submit"/>
Javascript
Javascript
<script>
function formSubmit()
{
document.getElementById("frm1").submit();
}
</script>
回答by pavan
consider the HTML:
考虑 HTML:
<form id="target" action="destination.html">
<input type="text" value="Hello there" />
<input type="submit" value="Go" />
</form>
<div id="other">
....
</div>
The event handler can be bound to the form:
事件处理程序可以绑定到表单:
$('#target').submit(function() {
alert('Handler for .submit() called.');
return false;
});
Click function:
点击功能:
$('#other').click(function() {
$('#target').submit();
});
Here is the link have a look: How can I submit form on button click when using preventDefault()?
回答by Samuel Liew
You can use ajax to submit the first form before the second one:
您可以使用ajax在第二个表单之前提交第一个表单:
$('form[name=frm2]').submit(function() {
var form1 = $('form[name=frm1]');
$.ajax({
type: "POST",
url: form1.attr('action'),
data: form1.serialize(),
async: false;
});
});
回答by David 'the bald ginger'
I usually avoid the .submit() function as i almost always have to do something more with response that .submit() would allow me to do.
我通常避免使用 .submit() 函数,因为我几乎总是需要对 .submit() 允许我做的响应做更多的事情。
Thus, a non .submit option where you SUBMIT buttons would have to be changed to normal buttons.
因此,您必须将提交按钮的非 .submit 选项更改为普通按钮。
$('.btn2').bind('click', function(){
var form1Data = $('#frm1').serialize();
$.ajax({
url : 'someurl',
type : 'post',
datatype : 'json',
data : form1Data,
success : function(json) {
sumbitForm2();
}
});
});
function submitForm2() {
var form2Data = $('#frm2').serialize();
$.ajax({
url : 'urlToSumbitForm1',
type : 'post',
datatype : 'json',
data : form2Data,
success : function(json) {
//do something if you need to
}
});
}