jQuery检测哪个按钮提交了表单 [英] jquery detect which button submitted form

查看:115
本文介绍了jQuery检测哪个按钮提交了表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下内容的表格:

I have a form with the following:

<form id="my-form" ...>
    ...
    <button type="submit" name="bttnsubmit" value="1">The first button</button>
    <button type="submit" name="bttnsubmit" value="2">The last button</button>
</form>

我想使用以下方法检测哪个触发了表单提交事件:

I'd like to detect which triggered the form submit event using just:

$('#my-form').submit(function(){
    //psuedo code
    if($('[name=bttnsubmit]').val() == 1) {
        ....
    }
});

显然,选择器将始终返回遇到的第一个bttnsubmit元素的值,因此我需要其他魔术选择器或过滤器之类的东西.

Obviously that selector will always return the value of the first bttnsubmit element it comes across, so I need some other magic selector or filter or something.

我已经看到$('[name=bttnsubmit][clicked=true]')吹捧过,但是在我的尝试中还没有奏效...

I have seen $('[name=bttnsubmit][clicked=true]') touted about but that has not yet worked in my attempts...

我当然可以使用$('[name=bttnsubmit]').click(),但希望能够在表单提交事件中实现我的目标.

I could of course resort to $('[name=bttnsubmit]').click() but would prefer to be able to achieve my goals in the forms submit event.

非常感谢您的帮助/提示.

Any help/tips much appreciated.

推荐答案

我不知道是否有任何内置事件数据(也许有),但是想到的一个想法是处理事件的点击事件.按钮并存储该值的全局引用.像这样:

I don't know if there is any built in event data (maybe there is) but one idea that comes to mind is to handle the click event of the buttons and store a global reference of the value. Something like this:

var ButtonValue;

$('button[type="submit"]').click(function(e){
   ButtonValue = $(this).val();
});

$('#my-form').submit(function(){
    //psuedo code
    if(ButtonValue == 1)
    {
        ....
    }
});

这篇关于jQuery检测哪个按钮提交了表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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