2

I have a JSP page, which has standard form on it. I have two buttons, each perform a different action when pressed, and the form is submitted - action 1 and action 2.

I originally had this set up for one button, so it was all done through the following and worked fine:

$('#form').submit( function() { .... }

But now I have two buttons, I want it to do the same, but how to find which button I pressed.

I could do this through the .click function, but I dont want to break my existing form.submit functionality.

Below is my code for this - which doesn't work:

    $('#form').submit( function() {

        // Set the field array variables with data
        $('button[name="action1"], [name="action2"]').each(function(index) {
            alert('index : ' + index );
            alert('value : ' + this.value);
        });

        $('button[name="action1"]').click(function(e) { 
            alert('ac1 clicked');
        }); 

        $('button[name="action2"]').click(function(e) { 
            alert('ac2 clicked');
        }); 

my html buttons are:

            <button id="submitButton" name="action1" value="action1" type="submit">action 1</button>
            <button id="submitButton" name="action2" value="action2" type="submit">action 2</button>

Is there a way I can do this inside my form.submit, or a way to do the .click, which then submits the form. I am a little lost for a solution on this?

Please help :)

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
babb
  • 423
  • 2
  • 15
  • 38

5 Answers5

2

You can read the related target of the event object.

$('#form').on('submit', function(evt) {
    if (evt.relatedTarget && $(relEl).is('input[type=submit]')) {
        /* related element is a button - do something */
    }
    evt.preventDefault(); //cancel form submit, as required
});
Mitya
  • 33,629
  • 9
  • 60
  • 107
0

In the button's click handler, set a hidden field before submitting the form. Then read the value of that hidden field in the request handler to find out which action was requested.

tdammers
  • 20,353
  • 1
  • 39
  • 56
0

Bind a event handler to your buttons

$('button').on('click', function(e) {
    var buttonId = $(this).attr('name');
    if(buttonId = 'action1') {
        // action1 was pressed
    } else {
        // action2 was pressed
    }

    $('#form').trigger('submit'); // trigger submit of form.

    e.preventDefault();
});
ninja
  • 2,233
  • 1
  • 15
  • 15
  • Hi @ninja This is working well for what I need. Only issue is, my form.submit has a function, i.e. `$('#form').submit( function() { ... }` How can I do this with the trigger submit you have suggested? – babb Aug 03 '12 at 12:27
0

For standard HTML form submission :

HTML:

<form method="..." action="...">
    ...
    <input type="hidden" name="action">
    <input value="action1" type="submit" value="action 1" />
    <input value="action2" type="submit" value="action 2" />
    ...
</form>

Javascript:

$('button[type="submit"]').on('click', function() {
    $("#action").val(this.value);//where "#action" selects an input field (in the same form) of type="hidden"
});

For AJAX submission, do the same but read the action field's value back into javascript in the submit handler.

Beetroot-Beetroot
  • 18,022
  • 3
  • 37
  • 44
0

First of, never include two dom elements with the same id on the same page. The class attribute is for such things. Change the id's of the buttons to submitButton1 and submitButton2 respectively and then this ought to work:

$('#submitButton1').closest('#form').submit(function() {
    // first button action
});

$('#submitButton2').closest('#form').submit(function() {
    // second button action
});
rthor
  • 128
  • 6