如何使用jquery .on()提交来检测输入按钮的值 [英] How to detect input button value with jquery .on() submit

查看:85
本文介绍了如何使用jquery .on()提交来检测输入按钮的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jquery .on()提交来检测在此表单上按下了哪个输入按钮?

How can I detect which input button is pressed on this form using jquery .on() submit?

<form name="input" action="none" method="post">
 <input type="text" name="user">
 <input id="submit" type="submit" value="Submit">
 <input id="cancel" type="submit" value="Cancel">
</form>​​​​​​​​​​​​​​​



$('body').on('submit', 'form', function(e) {
      console.log($(this).attr("value"));
      return false;})

一个jsfiddle示例在这里 http://jsfiddle.net/dansalmo/suZHg/

​ A jsfiddle example is here http://jsfiddle.net/dansalmo/suZHg/

我看过很多地方,并尝试了多种不同的方法来尝试检测.on()方法中的输入提交按钮值,但是我找不到它.

I have looked many places and tried many different ways of trying to detect the input submit button value within the .on() method but I can't get it.

在我的示例中,$(this).attr("value")和$(this).val()出现未定义

In my example $(this).attr("value") and $(this).val() come up undefined

然后,我希望能够使用ajax提交表单,因此仅检测按钮不是此问题的重点.如果无法使用.(on)提交来进行操作,那么了解这一点将很有帮助,而不是使用不涉及表单提交的备用按钮检测方法进行回答.

I then want to be able to submit the form using ajax so just detecting the button is not the point of this question. If there is no way to do it using .(on) submit then it would be helpful to know that as opposed to answering with alternate button detection methods that do not involve form submission.

推荐答案

我从提交处理程序中看不到任何获取此信息的方法,但是您可以从按钮单击事件中获取该信息,然后在提交事件.

I don't see any way from the submit handler to get this information, but you can get the information from the button click events and then use it in the submit event.

$('body').on('submit', 'form', function(e) {
      console.log($(this).data('clicked'));
      return false;
});
$('form').on('click', 'input[type=submit]', function(e) {
      $(this.form).data('clicked', this.value);
});

http://jsfiddle.net/suZHg/15/

这篇关于如何使用jquery .on()提交来检测输入按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆