Skip to content Skip to sidebar Skip to footer

Jquery - How To Get The Value Of An Input Of Type Submit When Ajaxifying A Form

Suppose I have the following code:

Solution 1:

In non-ajax forms the value of the submit button is only sent with the form, if it is clicked. So if you want to mimic this behaviour with ajax you have to do it something like this:

$("#submit_button").click(function() {
    var data = $("#my_form").serialize();
    data += data.length ? "&" : ""; // check if empty
    data += escape($(this).attr("name"))+"="+escape($(this).val());
    // ...returnfalse;
});

This appends the serialized data of the submit button to the serialized form data.

Solution 2:

This is by design. the .serialize() method does not serialize inputs of type submit.

Unfortunately the way you have it you would not be able to access the source of the event. Your best bet is to wire all the submits, and then append the source data like this:

$('input:submit').bind('click', function(){    

var data = $("#form1").serialize();
data += "&"+escape($(this).attr("name"))+"="+escape($(this).val());

alert(data);
$.post($('#form1').attr('action'), data, function(data) {
//do something
});

returnfalse;
});

Solution 3:

I am using the following solution:

<input type="radio" name="action" value="action_1" style="display:none"></input>
<button type="submit" onclick="this.previousElementSibling.checked=true" >Do Action 1</button>

You repeat this for any number of choices (name, value), and the form is submitted as soon as you choose one. Make sure the page is reloaded after use.

Post a Comment for "Jquery - How To Get The Value Of An Input Of Type Submit When Ajaxifying A Form"