jQuery .submit() 无论如何都不会执行
我有一个页面,其中包含两个表单,两个表单都有一个唯一的 ID。我需要在提交任一表单时使页面淡出。然而,这是行不通的。
这是代码
$(document).ready(function(){
$('#container').fadeOut(0);
$('#container').fadeIn(500);
});
$('form').submit(function(){
alert("Hello.");
$('#container').fadeOut(500);
});
正如您从代码中看到的,它应该显示警报,但它没有,甚至没有淡出页面。 我的表单包含一个设置为只读的文本输入和一个提交按钮。
提前致谢。
I have a page which contains two forms, both with an unique ID. I need to make the page fade out when either of the forms is submitted. However, it won't work.
Here's the code
$(document).ready(function(){
$('#container').fadeOut(0);
$('#container').fadeIn(500);
});
$('form').submit(function(){
alert("Hello.");
$('#container').fadeOut(500);
});
As you can see from the code, it should show an alert but it does not, and it doesn't even fade out the page.
My forms contain a text input, which is set to readonly, and a submit button.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
提交绑定在文档之外准备就绪,并且在执行时可能找不到表单标记。
jsfiddle 示例 - http://jsfiddle.net/KbaG3/
Try this:
The submit binding was outside of the document ready and it probably couldn't find the form tag when it was executed.
jsfiddle example - http://jsfiddle.net/KbaG3/
您可能应该将提交事件注册函数移至文档就绪函数内部,以便在注册该函数之前确保 DOM 已准备好。
如果这没有帮助,请查看页面上是否抛出任何可能会停止执行的 JavaScript 错误。
You should probably move the submit event register function to inside the document ready function so that you can be sure the DOM is ready before registering the function.
If that doesn't help, see if there are any javascript errors being thrown on the page which might halt the execution.
除非您将此 JS 包含在页面底部,否则应将
$(form)....
代码移至$(document).ready()
block这是因为当您将事件绑定到元素时,您的元素不存在
Unless you're including this JS at the bottom of the page, you should move the
$(form)....
code inside the$(document).ready()
blockThis is because your element doesn't exist when you are binding the event to it
尝试:
我认为这是一个更干净的代码,而且也没有冲突证明。
Try:
I think is a cleaner code and and also no conflict proof.