jQuery: how to get which button was clicked upon form submission?

后端 未结 26 1682
旧巷少年郎
旧巷少年郎 2020-11-22 16:01

I have a .submit() event set up for form submission. I also have multiple forms on the page, but just one here for this example. I\'d like to know which submi

26条回答
  •  暖寄归人
    2020-11-22 17:00

    Another possible solution is to add a hidden field in your form:

    
    

    Then in the ready function add functions to record what key was pressed:

    $('form#myForm #btnSubmit').click(function() {
        $('form#myForm #btaction').val(0);
    });
    
    $('form#myForm #btnSubmitAndSend').click(function() {
        $('form#myForm #btaction').val(1);
    });
    
    $('form#myForm #btnDelete').click(function() {
        $('form#myForm #btaction').val(2);
    });
    

    Now in the form submition handler read the hidden variable and decide based on it:

    var act = $('form#myForm #btaction').val();
    

提交回复
热议问题