Value of submit button clicked

后端 未结 6 2137
眼角桃花
眼角桃花 2021-02-07 08:58

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:



        
相关标签:
6条回答
  • 2021-02-07 09:37
    $('input[type="submit"]').click(function() {
        alert ($(this).val());
    }
    
    0 讨论(0)
  • 2021-02-07 09:43

    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:

    alert($(this).context.submit.value);
    
    0 讨论(0)
  • 2021-02-07 09:47

    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.

    0 讨论(0)
  • 2021-02-07 09:55

    This seems to get the clicked submit button:

    jQuery("#TheForm").context.activeElement.name
    jQuery("#TheForm").context.activeElement.value
    
    0 讨论(0)
  • 2021-02-07 09:59

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

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

    EDIT As of jQuery 1.7 live is deprecated. Use on instead.

    $(document).on("click", ":submit", function(e){
        alert($(this).val());
    });
    
    0 讨论(0)
  • 2021-02-07 09:59

    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.

    0 讨论(0)
提交回复
热议问题