Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I handle Two Submit buttons on One Form?

I have two submit buttons and one form. How do I check what submit button was selected in my jquery code?

<% using (Html.BeginForm("UserInfo", "Home", FormMethod.Post, new { id  = "formNext" })) { %> 
.... 
<input id="submitHome" type="submit" name="goHome" value="Home" />  
<input id="submitNext" type="submit" name="getNext" value="Next" /> 
<% } %>


$(document).ready(function() {       
$('#formNext').submit(function() {      
        //Code Does not work but looking at something like this...
        //$('#submitHome').click(function() {
        //      navigate to Home;
        //});
        //$('#submitNext').click(function() {
        //      return true;
        //});
    });
});
like image 446
MrM Avatar asked Jul 23 '10 19:07

MrM


2 Answers

$('#submitHome').click(function() {
      //navigate to Home;
});
$('#submitNext').click(function() {
      return true;
});

These should work if you pull them outside of the form.submit(). (right now those handlers are being attached after the form is submitted, which is too late since the click has already occurred)

like image 64
heisenberg Avatar answered Oct 10 '22 12:10

heisenberg


You can try something like this

$(function() {
 var buttonpressed;
 $('input[type=submit]').click(function() {
      buttonpressed = $(this).attr('name')
 })
 $('form').submit(function() {
      alert('button clicked was ' + buttonpressed)
        buttonpressed=''
    return(false)
 })
})

Source: https://forum.jquery.com/topic/determining-which-of-two-submit-buttons-were-clicked-in-a-single-form

like image 23
Jose Rojas Avatar answered Oct 10 '22 11:10

Jose Rojas