JQuery Ajax 验证后如何提交表单?
我无法让 .submit() jquery 函数工作 我已经将代码中的所有内容归结为:
<form action="/server/addserver.php" method="post" accept-charset="utf-8" id="mainform"><input type="text" name="test" value="" /><input type="submit" name="submit" value="Submit" id="submitbutton" /></form>
<script type="text/javascript">
$("#submitbutton").click(
function(){
$("#mainform").submit();
return false;
});
</script>
在我看来,这应该 1 - 停止按钮的默认行为,然后 2 - 手动提交表单。
如果我换出 .submit();与 .hide();有效。这不是在第一次运行一些 ajax 验证后手动提交表单的首选方法吗?
谢谢
I can't get the .submit() jquery function to work I've boiled down everything in my code to:
<form action="/server/addserver.php" method="post" accept-charset="utf-8" id="mainform"><input type="text" name="test" value="" /><input type="submit" name="submit" value="Submit" id="submitbutton" /></form>
<script type="text/javascript">
$("#submitbutton").click(
function(){
$("#mainform").submit();
return false;
});
</script>
Seems to me this should 1 - stop the default behavior of the button then 2 - manually submit the form.
If i swap out .submit(); with .hide(); that works. Is this not the preferred way to manually submit a form after first running some ajax validation ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做:
这将在提交表单时运行(只需使用普通的
按钮)。此处返回 false 将阻止页面发布。
如果您想将事件绑定到按钮,请尝试如下:
You can do:
This will run when the form is submitted (just use the normal
<input type="submit" />
button). Returning false here will prevent the page from posting.If you wanted to bind the event to your button, try it like this:
首先,我认为您应该注意以下几点:
任何时候您想像这样立即将事件绑定到 DOM 元素,您应该确保页面已完全加载。如果有疑问,请将代码包装在
。
此外,如果 input[type="submit"] 元素的 name 属性设置为,则 .trigger("submit") 方法不起作用值“提交”。由于不带参数调用 .submit() 是 .trigger('submit') 的快捷方式,因此无论何时调用 JQuery 的 .submit() > 元素上的方法,您需要确保并分配输入的 name 属性,而不是“submit”。
现在,回答您的问题;首先关于返回 false 是否是手动提交表单的首选方式,答案通常是否定的。原因是返回 false 会执行三件事,而您通常不想执行所有这些操作。它可以防止事件的默认浏览器行为,还可以阻止事件冒泡,并且立即返回并退出该函数。通常,您可能只想event.preventDefault()或event.stopPropagation()…甚至完全取消绑定事件。但在这种情况下,在我看来,返回 false 而不是调用适当的晚上方法似乎会混淆代码的意图。
如果您还需要阻止浏览器对 submit 事件执行默认操作,则可以使用 JQuery 的备用触发方法:
.
Though, the validator method itself should probably be where you call form.submit() in the case that there are no errors. It should also be where you handle form submission, since it is the gatekeeper, so to speak.
总而言之,如果您要使用验证器插件,您应该遵循它自己的约定。通过将 name="submit" 添加到提交按钮,确保表单执行默认提交,然后删除您拥有的所有 JQuery 代码,并将其替换为验证器文档中的此示例:
这应该允许插件按预期运行。当然,最后一步是添加验证规则。我希望这有帮助。如果您在事件处理方面仍然遇到问题,请尝试使用 JQuery 的事件对象方法在控制台上进行调试。
First, there are a couple things I think you should be aware of:
Anytime you want to bind an event to a DOM element right off the bat like that, you should make sure the page has fully loaded. If in doubt, wrap your code inside
.
Also, the .trigger("submit") method doesn't work if the name attribute of the input[type="submit"] element is set to the value "submit". Since .submit() called with no arguments is a shortcut for .trigger('submit'), then anytime you call JQuery's .submit() method on an element, you would want to make sure and assign the input's name attribut something other than "submit".
Now, to answer your questions; first about whether returning false is the preferred way to manually submit a form, the answer is usually no. The reason is that returning false does three things, and you usually don't want to do all of them. It prevents the default browser behavior for the event, it also stops the event from bubbling, and, it immediately returns, exiting the function. Usually, you probably just want to either event.preventDefault() or event.stopPropagation()… or even unbind the event altogether. In this case though, in my opinion, returning false rather than calling the appropriate evening methods seems to muddle the intention of your code.
If you also need to prevent the default browser action on the submit event, you can use JQuery's alternate trigger method:
.
Though, the validator method itself should probably be where you call form.submit() in the case that there are no errors. It should also be where you handle form submission, since it is the gatekeeper, so to speak.
All in all, if you're going to the Validator plugin, you should follow it's own conventions. Ensure the form does a default submit by adding name="submit" to your submit button and then remove all the JQuery code you have and replace it with this example from the Validator documentation:
That should allow the plugin to operate as expected. The last step would be to add your validation rules, of course. I hope that helps. If you still have trouble with eventing, try using JQuery's Event object methods for debugging on the console.