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

后端 未结 26 1357
旧巷少年郎
旧巷少年郎 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 16:49
    $("form input[type=submit]").click(function() {
        $("<input />")
            .attr('type', 'hidden')
            .attr('name', $(this).attr('name'))
            .attr('value', $(this).attr('value'))
        .appendTo(this)
    });
    

    add hidden field

    0 讨论(0)
  • 2020-11-22 16:50

    Here's the approach that seems cleaner for my purposes.

    First, for any and all forms:

    $('form').click(function(event) {
      $(this).data('clicked',$(event.target))
    });
    

    When this click event is fired for a form, it simply records the originating target (available in the event object) to be accessed later. This is a pretty broad stroke, as it will fire for any click anywhere on the form. Optimization comments are welcome, but I suspect it will never cause noticeable issues.

    Then, in $('form').submit(), you can inquire what was last clicked, with something like

    if ($(this).data('clicked').is('[name=no_ajax]')) xhr.abort();
    
    0 讨论(0)
  • 2020-11-22 16:51

    For me, the best solutions was this:

    $(form).submit(function(e){
    
       // Get the button that was clicked       
       var submit = $(this.id).context.activeElement;
    
       // You can get its name like this
       alert(submit.name)
    
       // You can get its attributes like this too
       alert($(submit).attr('class'))
    
    });
    
    0 讨论(0)
  • 2020-11-22 16:52

    This one worked for me

    $('#Form').submit(function(){
    var btn= $(this).find("input[type=submit]:focus").val();
    alert('you have clicked '+ btn);
    
    }
    
    0 讨论(0)
  • 2020-11-22 16:54

    Similar to Stan answer but :

    • if you have more than one button, you have to get only the first button => [0]
    • if the form can be submitted with the enter key, you have to manage a default => myDefaultButtonId

    $(document).on('submit', function(event) {
        event.preventDefault();
        var pressedButtonId = 
             typeof $(":input[type=submit]:focus")[0] === "undefined" ? 
             "myDefaultButtonId" :
             $(":input[type=submit]:focus")[0].id;
        ...
     }
    
    0 讨论(0)
  • 2020-11-22 16:55

    I also made a solution, and it works quite well:
    It uses jQuery and CSS


    First, I made a quick CSS class, this can be embedded or in a seperate file.

    <style type='text/css'>
        .Clicked {
            /*No Attributes*/
        }
    </style>
    


    Next, On the click event of a button within the form,add the CSS class to the button. If the button already has the CSS class, remove it. (We don't want two CSS classes [Just in case]).

        // Adds a CSS Class to the Button That Has Been Clicked.
        $("form :input[type='submit']").click(function () 
        {
            if ($(this).hasClass("Clicked"))
            {
                $(this).removeClass("Clicked");
            }
            $(this).addClass("Clicked");
        });
    


    Now, test the button to see it has the CSS class, if the tested button doesn't have the CSS, then the other button will.

        // On Form Submit
        $("form").submit(function ()
        {
            // Test Which Button Has the Class
            if ($("input[name='name1']").hasClass("Clicked"))
            {
                // Button 'name1' has been clicked.
            }
            else
            {
               // Button 'name2' has been clicked.
            }
        });
    

    Hope this helps! Cheers!

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