javascript <form> 标签外的提交按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15686585/
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-10-27 01:41:54  来源:igfitidea点击:

Submit button outside the <form> tags

javascriptjqueryforms

提问by Peter

I've been searching for two hours and I couldn't find any solutions.

我已经搜索了两个小时,但找不到任何解决方案。

I would like to have two submit buttons, one inside the < form > tag and one outside the < form > tag.

我想要两个提交按钮,一个在 <form> 标签内,一个在 <form> 标签外。

<form id="example" name="example" action="post">
    Input <input type="text" name="text" />
    <input type="submit" name="submit" value="submit" />
</form>

<div class="button">Submit</div>

JS / jQuery

JS / jQuery

$(".button").click( function() {
    alert("Button clicked");
}); 

How can I send the form with the ('.button') class outside the form ?

如何在表单外发送带有 ('.button') 类的表单?

JS Fiddle here: http://jsfiddle.net/ZQLXb/

JS小提琴在这里:http: //jsfiddle.net/ZQLXb/

回答by Matt Cain

Use the submit()function to trigger the form submission:

使用submit()函数触发表单提交:

$(".button").click( function() {
    $('#example').submit();
});

You can also use the trigger()function:

您还可以使用该trigger()功能:

$(".button").click( function() {
    $('#example').trigger('submit');
});

Although they do exactly the same thing.

虽然他们做的事情完全一样。

回答by bipen

use submit()function to submit the form

使用submit()函数提交表单

$(".button").click( function() {
   $('#example').submit();
}); 

回答by Rahul Sahu

$(".button").bind("click",function() {
    $('#example').submit();
}); 

回答by davids

You could trigger the submitevent on the form:

您可以submit在表单上触发事件:

$('#example').trigger('submit')

回答by jmoerdyk

$(".button").click( function() {
    $('#example').submit();
});