HTML部分
<form>
<input type="text" value="" name="first_value" id="first_value" />
<input type="text" value="" name="second_value" id="second_value" />
<button id="submitForm" >Submit</button>
</form>
AJAX部分 呼叫通過點擊按鈕的功能。 然後通過使用$("input[id=first_value]").val()
或$("#first_value").val()
收集您的表單數據。
$('#submitForm').click(function(event){
// get the form data
// there are many ways to get this data using jQuery (you can use the class or id also)
var formData = {
'first' : $("input[id=first_value]").val(),
'second' : $("input[id=first_value]").val(),
};
// process the form
var ajaxResponse = $.ajax({
type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
url : 'someURL', // the url where we want to POST
data : JSON.stringify(formData),
contentType :'application/json',
error : function(data,status,error){
console.log(data+': '+status+': '+error);
},
success : function(status) {
//DO something when the function is successful
}
}).done(function(apiResponse) {
//Do something when you are done
});
});
來源
2014-01-14 22:37:46
Geo