我试图在输入邮政编码后自动提交表单,这将触发谷歌地图的显示。
如果我在表单中使用提交按钮,代码就能正常工作。表单提交,ajax代码完成它的工作,div被替换为对应于邮政编码的google地图。
但是,如果我尝试使用以下代码自动提交表单:
`// prepare the form when the DOM is ready $(document).ready(function() { var options = { target: '#htmlExampleTarget' // target element(s) to be updated with server response` // other available options:
//url: url // override for form's 'action' attribute
//type: type // 'get' or 'post', override for form's 'method' attribute
//dataType: null // 'xml', 'script', or 'json' (expected server response type)
//clearForm: true // clear all form fields after successful submit
//resetForm: true // reset the form after successful submit
// $.ajax options can be used here too, for example:
//timeout: 3000
};
// bind to the form's submit event
$('#htmlForm').submit(function() {
// inside event callbacks 'this' is the DOM element so we first
// wrap it in a jQuery object and then invoke ajaxSubmit
$(this).ajaxSubmit(options);
// !!! Important !!!
// always return false to prevent standard browser submit and page navigation
return false;
}); });
<form id="htmlForm" action="html-echo.php" method="post">
Message: <input type="text" name="message" onKeyUp="if(this.value.length>4)this.form.submit()">
<div id="htmlExampleTarget">
<script type="text/javascript">
$(function()
{
$("#map").gMap({ address: "Los Angeles", zoom: 13 });
});
</script>
<div id="map" style="width: 547px; height: 320px; border: 1px solid #777; overflow: hidden;"></div>
</div>所发生的一切就是加载表单操作(action=“html- page .php”)页面。
如何才能使this.form.submit的行为与提交按钮相同?
谢谢!
发布于 2011-04-15 17:46:02
DOM方法form.submit()不会触发提交处理程序。由于您使用的是jQuery,因此请改用$(this.form).trigger("submit")。
https://stackoverflow.com/questions/5673626
复制相似问题