0

I am trying to put multiple buttons on single form. Based on submit button, I want to process different action to form

<script type="text/javascript">
    $("#<portlet:namespace/>Form").submit(function(e) {
        //alert("test");
            var action = ???
        if (action == 'Add') {
            alert("add");
        } else if (action == 'Edit') {
            alert("edit");
        } else if (action ==  'Delete') {
            alert("delete");
        } else{
            alert("error");
        }
    });
</script>


<form name="<portlet:namespace/>Form" method="POST">
    <table>
        <tr>
            <td><input type="submit" name="action" value="Add"></input></td>
            <td><input type="submit" name="action" value="Edit"></input></td>
            <td><input type="submit" name="action" value="Delete"></input></td>
        </tr>
    </table>
</form>

How do I get which button triggered the submission of form?

RaceBase
  • 18,428
  • 47
  • 141
  • 202

2 Answers2

1
$(document).ready(function() {
    $("form").submit(function() { 

    var val = $("input[type=submit][clicked=true]").val()

    // DO WORK

});

I think U can then check val for different button names.

U can follow this : jQuery: how to get which button was clicked upon form submission?

it has similarity with your requirement.

Community
  • 1
  • 1
0

You can also bind event on input=submit

For example

$("#<portlet:namespace/>Form input[type=submit]").submit(function(e) {
    alert( $(this).val() );
    return false;
});
dododo
  • 256
  • 1
  • 6