我很想使用jQuery验证器插件来验证我的代码,但我想禁用自动提交我的表单。我宁愿使用jQuery $.post
方法自己发送它。jQuery验证 - 如何防止自动提交?
事实上,我不太确定为什么我的表单正在提交考虑到我的按钮不是submit
类型,而只是<button></button>
。
<form id="my_form" name="my_form" method="post" action="">
...
<button id="previous_button" class="nav-button">Previous</button>
<button id="next_button" class="nav-button">Next</button>
</form>
和我的onClick监听器,在我希望的有效输入,我可以张贴表单数据,然后移动到一个新的页面,否则让用户看到`error_messages'对话框,在其中重新定位窗口我的所有错误信息都显示出来。
$('#next_button').click(function(event) {
validateAndSave();
});
function validateAndSave() {
if($('#my_form').valid()) {
$.post('save_form_to_database.php',
$('#my_form').serialize());
window.location = 'next_page.php';
} else {
// reposition to see the error messages
window.location = '#error_messages';
}
}
这虽然结果(和结果是一样的debug
是否设置为true或false)是在有效的输入,我可以通过查看状态栏看到'next_page.php”闪烁然后我又回到原来的页面。同样在验证失败的情况下,我的页面似乎没有正确地重新定位。
所以我的问题是:
为什么我的网页被重定向回到原来的页面?
如何使用验证器进行验证,然后使用$ .post以我自己的方式发布表单?
非常感谢提前。
更新感谢响应
根据this page on the button element:
按钮的TYPE属性指定的那种按钮和取值提交(默认值),重置或按钮
所以我的按钮默认值为type="submit"
非常感谢您的帮助。我已更新我的回复,以包含指向按钮元素的更多信息的链接。我没有意识到它默认是'type =“submit”'。再次欢呼。 – Joe
感谢您的这一点,我挣扎了几个小时 –