jQuery submit, how can I know what submit button was pressed?

前端 未结 8 1596
盖世英雄少女心
盖世英雄少女心 2020-12-14 23:43

I\'m using ajaxSubmit plugin to send Ajax forms, but for some reason this plugin doesn\'t send names/values of input[type=image]\'s. So now I\'m catching the su

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 00:34

    Following is what I think would be a more comprehensive example of how to accomplish what you asked. You should replace the "#your-form" with your id form and replace "Button One" & "Button Two" with the values on your form buttons.

    $(document).ready(function() {
      var target = null;
      $('#your-form-id :input').focus(function() {
        target = $(this).val();
      });
      $('#your-form-id').submit(function() {
    
        if (target == 'Button One') {
          alert (target);
        }
        else if (target == 'Button Two') {
          alert (target);
        }
      });
    });
    

提交回复
热议问题