Javascript 如何在jQuery中找到特定表单中的提交按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2892683/
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 find the submit button in a specific form in jQuery
提问by leifg
I try to get jQuery object of a submit button in a specific form (there are several forms on the same page).
我尝试以特定表单(同一页面上有多个表单)获取提交按钮的 jQuery 对象。
I managed to get the form element itself. It looks something like this:
我设法获得了表单元素本身。它看起来像这样:
var curForm = curElement.parents("form");
The current Element has the context HTMLInputElement. The several techniques I tried to get the according submit element of the form:
当前元素具有上下文 HTMLInputElement。我试图获得表单的相应提交元素的几种技术:
var curSubmit = curForm.find("input[type='submit']");
var curSubmit = $(curForm).find("input[type='submit']");
var curSubmit = curForm.find(":submit");
var curSubmit = $(curForm).find(":submit");
var curSubmit = $(curSubmit, "input[type='submit']");
the result is always the same (and very strange). The result that I get is the same element as "curElement".
结果总是一样的(而且很奇怪)。我得到的结果是与“curElement”相同的元素。
So how can I get the right submit button?
那么我怎样才能获得正确的提交按钮呢?
回答by SLaks
The following should work:
以下应该工作:
var submit = curElement.closest('form').find(':submit');
回答by jigfox
This should work:
这应该有效:
var curSubmit = $("input[type=submit]",curForm);
EDIT:Note the missing 'in the selector
编辑:注意'选择器中的缺失
回答by Jakub Kukul
Using plain javascript (without relying on jquery):
使用普通的 javascript(不依赖 jquery):
var curSubmit = curForm.querySelector('button[type="submit"]');
回答by Ahmed Al Bermawy
This works for me:
这对我有用:
var curSubmit = $("input[type=submit]",this);
where thismean current form submitted
其中,这意味着目前的形式提交
for ex. to get the name of submitted button and all inputs submitted
例如。获取提交按钮的名称和提交的所有输入
$( "form" ).on( "submit", function( event ) {
event.preventDefault();
var data = $(this).serialize(); //all input variables
console.log(data); //print data in console
var submit = $("input[type=submit]",this).attr('name');
alert(submit); // name of submit button
});
回答by Sergey Ratnikov
Because a HTML5 submit button may be out of form tag http://www.w3.org/TR/html-markup/input.submit.html#input.submit.attrs.form, you can use the following code to find it:
因为 HTML5 提交按钮可能是形式标签http://www.w3.org/TR/html-markup/input.submit.html#input.submit.attrs.form,您可以使用以下代码找到它:
$(curElement.closest('form').get(0).elements).filter(':submit')
回答by outofmind
In case you want to find the submit button of the form after it was submitted, you may find the following useful ... I use it to disable the submit button after the form was submitted to prevent multiple clicks.
如果您想在提交后找到表单的提交按钮,您可能会发现以下有用......我使用它在提交表单后禁用提交按钮以防止多次点击。
$("form").submit(function () {
if ($(this).valid()) { // in case you have some validation
$(this).find(":submit").prop('disabled', true);
$("*").css("cursor", "wait"); // in case you want to show a waiting cursor after submit
}
});
回答by SpeCT
BTW: Last selector looks weird. It selects each curSubmit(hm?) in every input[type=submit]tag. May be you mean var curSubmit = $("input[type=submit]", curForm);
顺便说一句:最后一个选择器看起来很奇怪。它选择每个input[type=submit]标签中的每个 curSubmit(hm?) 。可能是你的意思var curSubmit = $("input[type=submit]", curForm);

