HTML 表單
<form name="ajaxform" id="ajaxform" action="ajax-form-submit.php" method="POST">
First Name:
<input type="text" name="fname" value="" /> <br />
Last Name:
<input type="text" name="lname" value="" /> <br />
Email :
<input type="text" name="email" value="" /> <br />
</form>
jQuery
<script>
$(document).ready(function(e) {
//表單發送處理
$("#ajaxform").submit(function(e) {
$.ajax({
type: $(this).attr("method"),
url: $(this).attr("action"),
data: $(this).serialize() + '&time=' + $.now(),
dataType: "json"
})
.done(function(data) {
console.log(data.data);
})
.fail(function(jqxhr, textStatus, error) {
alert('失敗');
});;
e.preventDefault(); //停止預設動作
//e.unbind(); //解除綁定。停止多個表單提交.
});
});
</script>
延伸閱讀
http://hayageek.com/jquery-ajax-form-submit/