欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

form multi two submit button get click button 得到点击的 submit 按钮

286down voteaccepted



Two submit buttons in one form

I have two submit buttons in a form. How do I determine which one was hit serverside?



17 Answers  正确答案

If you give each one a name, the clicked one will be sent through as any other input.

<input type="submit" name="button_1" value="Click me">
shareimprove this answeranswered Feb 13 '09 at 21:48Greg231k42325315

68 Also make sure the name of the button has correct name! For example "button-1" would NOT work. May save someone lots of hassle so keep this in mind. – pdolinaj Jan 22 '13 at 14:00 13 Normally, all inputs in the form are sent with the form. Since a button's value is submitted only if clicked, you'd have to search the form values for these pre-defined names. I think the other answer (stackoverflow.com/a/21778226/88409) that involves giving them all the same name, with different values, makes more sense. Then you just grab the value under a single known form field name. It also makes it more obvious that only one value (the clicked one) will be sent for the given input name, much like how radio buttons work (same name, different values). – Triynko Nov 10 '15 at 19:06    @Triynko , as Robin Green said in the comments of that answer, this one is better for internationalization. For example, if the page is rendered in Spanish, the text of the buttons will likely be different. So having the logic of your code depend on the text of that button will break in that case. Going by the name is safer, as it is a value that is not displayed to the user and therefore can be treated more as a "private" variable and less as a message to users. – sfarbota Sep 5 at 21:40


来自 https://stackoverflow.com/questions/547821/two-submit-buttons-in-one-form

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

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 submit button was clicked without applying a .click() event to each one.

Here's the setup:

<html><head>
  <title>jQuery research: forms</title>
  <script type='text/javascript' src='../jquery-1.5.2.min.js'></script>
  <script type='text/javascript' language='javascript'>
      $(document).ready(function(){
          $('form[name="testform"]').submit( function(event){ process_form_submission(event); } );
      });
      function process_form_submission( event ) {
          event.preventDefault();
          //var target = $(event.target);
          var me = event.currentTarget;
          var data = me.data.value;
          var which_button = '?';       // <-- this is what I want to know
          alert( 'data: ' + data + ', button: ' + which_button );
      }
  </script></head><body><h2>Here's my form:</h2><form action='nothing' method='post' name='testform'>
  <input type='hidden' name='data' value='blahdatayadda' />
  <input type='submit' name='name1' value='value1' />
  <input type='submit' name='name2' value='value2' /></form></body></html>

Live example on jsfiddle

Besides applying a .click() event on each button, is there a way to determine which submit button was clicked?

shareimprove this questionedited Feb 10 '16 at 10:49VKatz2,82921735asked Apr 19 '11 at 19:39hawkexp943274

1 The irony being of course that this information is trivial to determine server-side. – Neil Apr 19 '11 at 19:522 @Neil Not if you are submitting the form via $.ajax() and a serializeArray() on the form. – Aaron Apr 15 '13 at 0:18   possible duplicate of How can I get the button that caused the submit from the form submit event? – Bergi Jun 10 '13 at 18:22

23 Answers 

正确答案

up vote187down voteaccepted

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

shareimprove this answeredited May 23 at 12:02Community♦11answered Apr 19 '11 at 19:43hunter48.7k1094102

8 +1 Nice solution. You might want to add a statement that resets the clicked attribute to false across the buttons in case the form submit is handled in an ajax way and you want to avoid getting previsouly clicked button again. – Chandu Apr 19 '11 at 19:45    Oh, I see. You're adding your own "clicked" attribute. I was looking all over for a "clicked" boolean and couldn't find one anywhere. I never thought of making one myself. Good idea! – hawkexp Apr 19 '11 at 20:33    Do you know which version of jQuery started supporting this? It doesn't seem to work on 1.4.4 which the app I'm working on is unfortunately stuck on... – Alex Kinnee Jun 4 '12 at 13:07   Any decent version will do. If your version doesn't work, either replace it or ruuuuun! – Fabio Milheiro Jan 19 '14 at 10:506 Be aware that this only works with input elements, not button elements. – Bob.at.AIPsychLab Jun 14 '15 at 23:11

来自 https://stackoverflow.com/questions/5721724/jquery-how-to-get-which-button-was-clicked-upon-form-sub...

普通分类: