javascript JQuery `live()` 和 `submit()` 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4376865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery `live()` and `submit()` problem
提问by Nick Craver
I wanna do something like this, but this one looks like happing for infinite times.
我想做这样的事情,但这个看起来像是发生了无限次。
$("form").live("submit", function() {
if($(this).attr('action') != "ajax"){
$(this).submit();
return true; // even i do this!! but form is NOT submited!!
}
else { /* doing the ajax stuff! */ }
});
in Chrome and Firefox after a while the form gets submitted, something like 10seconds and in IE it crashes !
一段时间后,在 Chrome 和 Firefox 中,表单被提交,大约 10 秒,而在 IE 中它崩溃了!
I know when i say form.submit means that i am submitting this and get called function again and again, how can i avoid this ?
我知道当我说 form.submit 意味着我正在提交这个并一次又一次地被调用函数时,我该如何避免这种情况?
回答by Nick Craver
By fring .submit()again, which bubbles back to the .live()handler, you're causing an infinite loop, instead you want to call the native form.submit()method here, like this:
.submit()再次fring返回到.live()处理程序,您将导致无限循环,而不是您想在form.submit()此处调用本机方法,如下所示:
$("form").live("submit", function() {
if(this.action != "ajax") {
this.submit();
}
else { /* doing the ajax stuff! */ }
});
Also .actionis a native DOM property, you can access it that way...not need for the jQuery overhead here.
也是.action本机 DOM 属性,您可以通过这种方式访问它……这里不需要 jQuery 开销。

