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

后端 未结 26 1652
旧巷少年郎
旧巷少年郎 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:42

    I asked this same question: How can I get the button that caused the submit from the form submit event?

    I ended up coming up with this solution and it worked pretty well:

    $(document).ready(function() {
        $("form").submit(function() { 
            var val = $("input[type=submit][clicked=true]").val();
            // DO WORK
        });
        $("form input[type=submit]").click(function() {
            $("input[type=submit]", $(this).parents("form")).removeAttr("clicked");
            $(this).attr("clicked", "true");
        });
    });
    

    In your case with multiple forms you may need to tweak this a bit but it should still apply

提交回复
热议问题