jQuery提交,我怎么知道按下了什么提交按钮?

new*_*bie 10 jquery plugins submit

我正在使用ajaxSubmit插件来发送Ajax表单,但由于某种原因,这个插件不会发送input[type=image]'s的名称/值.所以现在我在ajaxSubmit处理表单之前捕获提交事件,我需要知道是否有可能找出按下了什么按钮?

kar*_*m79 19

这将捕获发起提交的任何输入元素:

$(document).ready(function() {
    var target = null;
    $('#form :input').focus(function() {
        target = this;
        alert(target);
    });
    $('#form').submit(function() {
        alert(target);
    });
});
Run Code Online (Sandbox Code Playgroud)


cus*_*pvz 7

$("input .button-example").click(function(){
//do something with $(this) var
});
Run Code Online (Sandbox Code Playgroud)

PS:你有jQuery控制$ var吗?否则你必须这样做:

jQuery.noConflict();
jQuery(document).ready(function(){
    jQuery("input .button-example").click(function(){
    //do something with jQuery(this) var
       alert(jQuery(this));
    });
});
Run Code Online (Sandbox Code Playgroud)

如果你不控制事件(表格提交)

$(document).ready(function(){
    $("#formid").submit(function() {
          alert('Handler for .submit() called.');
          return false;
    });
});
Run Code Online (Sandbox Code Playgroud)

告诉我一些是否有效;)


tha*_*smt 5

这就是我正在使用的(其他人略有不同,使用mouseup和keyup事件而不是焦点):

var submitButton = undefined;
$('#your-form-id').find(':submit').live('mouseup keyup',function(){
    submitButton  = $(this);
});
$('#your-form-id').submit(function() {
    console.log(submitButton.attr('name'));
});
Run Code Online (Sandbox Code Playgroud)