首先,您的表单应如下所示:
<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();
});