问题
i want to submit a form with about 10 input using jquery/ajax,but i don't know how can i pass the data to it through data parameters of ajax.should i serialize them ?
回答1:
The jQuery.serialize can be helpful for you. It is important that you use name
property for all fields of the form which you want to submit. The corresponding code can be about the following
$("form#myFormId").submit(function() {
var mydata = $("form#myFormId").serialize();
console.log(mydata); // it's only for test
$.ajax({
type: "POST",
url: "myUrlToPostData.php",
data: mydata,
success: function(response, textStatus, xhr) {
console.log("success");
},
error: function(xhr, textStatus, errorThrown) {
console.log("error");
}
});
return false;
});
回答2:
$.ajax({
type: "POST",
url: "handle.php",
data: "n="+name+"&e="+email,
success: function() {
alert('It worked');
}
});
Here is the simplest way to use it. This would just send two post variables, $_POST['n'] and $_POST['e'], to handle.php. Your form would look like this if your ajax is in a function called send():
<form id="form" onsubmit="send(this.name.value, this.email.value); return false;">
来源:https://stackoverflow.com/questions/5929790/passing-data-in-form-submission-via-jquery-ajax