jQuery 单击提交按钮的值

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

Value of submit button clicked

jquery

提问by amit_g

This should be really straight forward.

这应该是非常直接的。

I'm checking if a form is being submitted using jquery. The form has multiple submit buttons with various values:

我正在检查是否正在使用 jquery 提交表单。该表单有多个具有不同值的提交按钮:

<button type="submit" value="foo">Foo</button>
<button type="submit" value="bar">Bar</button>

I would like to find the value of the button that just submitted the form:

我想找到刚刚提交表单的按钮的值:

$(form).live('submit', function() {
    // Get value of submit button
}

Thanks

谢谢

回答by amit_g

Bind the event to submit buttons instead of the form and then $(this) would have the reference to the buttons...

绑定事件以提交按钮而不是表单,然后 $(this) 将引用按钮...

$(":submit").live('click', function() {
    alert($(this).val());
})

EDITAs of jQuery 1.7 liveis deprecated. Use oninstead.

编辑jQuery 1.7 live已被弃用。使用代替。

$(document).on("click", ":submit", function(e){
    alert($(this).val());
});

回答by Mattias

This seems to get the clicked submit button:

这似乎得到了点击提交按钮:

jQuery("#TheForm").context.activeElement.name
jQuery("#TheForm").context.activeElement.value

回答by Furicane

You can't if you're using the "submit" event. A form can be submitted without ever hitting a submit button.

如果您使用的是“提交”事件,则不能。无需点击提交按钮即可提交表单。

In order to obtain which button was clicked, you need to alter your approach by using custom events and marking down which button was actually clicked (creating a selector that can catch click on any of the X buttons in the form). That also requires binding a custom event and prohibiting submit event of the form itself.

为了获得点击了哪个按钮,您需要通过使用自定义事件并标记实际点击了哪个按钮来改变您的方法(创建一个可以捕获表单中任何 X 按钮的点击的选择器)。这还需要绑定自定义事件并禁止表单本身的提交事件。

回答by Grim...

$('input[type="submit"]').click(function() {
    alert ($(this).val());
}

回答by CrazyPyro

I ended up using the click event on each submit button, as others have suggested. That works fine, and you can return false from them if you want to cancel the submit. Just don't try to also bind the submit event of the form. You could also have each of these handlers call a common submit-handler function, passing their own value as an argument.

正如其他人所建议的那样,我最终在每个提交按钮上使用了点击事件。这很好用,如果你想取消提交,你可以从他们那里返回 false。只是不要尝试同时绑定表单的提交事件。您还可以让这些处理程序中的每一个都调用一个公共的提交处理程序函数,将它们自己的值作为参数传递。

回答by Rui Seixas Monteiro

I recommend the use of the Firebug Add On, you get a lot of responses to your questions just looking to the data in the console. I realize that in your case you can access the submit value this way:

我推荐使用 Firebug Add On,只要查看控制台中的数据,您就会得到很多问题的答案。我意识到在您的情况下,您可以通过以下方式访问提交值:

alert($(this).context.submit.value);