所有形式的首先應該是這個樣子:
<form action="process.php" method="post" id="myForm">
<select name="site" id="site">
<option>Apple</option>
<option>Google</option>
<option>Microsoft</option>
</select>
<input type="hidden" name="date" value="01/05/2012" />
<input type="hidden" name="city" value="London" />
<input type="hidden" name="address" value="[... address ...]" />
</form>
然後通過AJAX提交您將使用serialize()
方法收集表單數據:
$("#site").on("change", function() {
var $form = $("#myForm");
var method = $form.attr("method") ? $form.attr("method").toUpperCase() : "GET";
$.ajax({
url: $form.attr("action"),
data: $form.serialize(),
type: method,
success: function() {
// do stuff with the result, if you want to
}
});
});
另外,如果你不不想使用AJAX,只需提交標準表格,就可以觸發表單提交,如下所示:
$("#site").on("change", function() {
$("#myForm").submit();
});
退房http://api.jquery.com/jQuery.ajax/爲Ajax請求 –