Javascript jQuery更改事件被调用两次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5464695/
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 change event being called twice
提问by Israel
I have a form with some input and select boxes, each has class="myClass"
. I also have the following script:
我有一个带有一些输入框和选择框的表单,每个框都有class="myClass"
. 我还有以下脚本:
$(document).ready(function() {
$(".myClass").change(function() {
alert('bla');
})
});
I dont understand why after each change in select box or input box, this function is being called twice. What's wrong here?
我不明白为什么每次更改选择框或输入框后,这个函数都会被调用两次。这里有什么问题?
Appreciate your help!
感谢你的帮助!
回答by Marc Uberstein
All I can think of is that you used the same class on the form itself. if so, remove the myClass style from your form tag.
我能想到的就是您在表单本身上使用了相同的类。如果是这样,请从表单标记中删除 myClass 样式。
Corrected : http://jsfiddle.net/rY6Gq/1/
更正:http: //jsfiddle.net/rY6Gq/1/
Faulty one with double alert: http://jsfiddle.net/rY6Gq/
有双重警报的错误:http: //jsfiddle.net/rY6Gq/
回答by jwebb
e.stopImmediatePropagation(); is what worked for me.
e.stopImmediatePropagation(); 是什么对我有用。
$(document).ready(function() {
$(".myClass").change(function(e) {
e.stopImmediatePropagation();
alert('bla');
})
});
回答by diego matos - keke
Its a bug, You'd add
它的一个错误,你会添加
$("#some_id").unbind('change');
before any change call
在任何更改调用之前
回答by user996015
if this occurred in IE, it may be this bug as it was for me: http://bugs.jquery.com/ticket/6593
如果这发生在 IE 中,它可能是我的错误:http: //bugs.jquery.com/ticket/6593
updating to jQuery 1.7.1 worked for me.
更新到 jQuery 1.7.1 对我有用。
回答by Selay
It happens when the same class or whatever attribute you are binding also has the same name parent or child. Obviously, when you change a child, parent also gets changed (its child changes). If they have the same class or attribute, it should fire twice. For example, in the following if you bind to "myClass", it will be called twice.
当您绑定的同一个类或任何属性也具有相同的父级或子级名称时,就会发生这种情况。显然,当您更改子项时,父项也会更改(其子项更改)。如果它们具有相同的类或属性,则应触发两次。例如,在下面如果您绑定到“myClass”,它将被调用两次。
<div class="myclass">
<select class="myClass"> </select>
</div>
回答by ?ime Vidas
It isn't: http://jsfiddle.net/nfcAS/
回答by Neha
The only one that worked for me was unbind before the change check.
唯一对我有用的是在更改检查之前解除绑定。
$(".select2Component").unbind();
$(".select2Component").change(function() {
//code
});
回答by jAntoni
For me - I had written the on change event
inside a function.
Moving it to $(document).ready(function () {});
solved my case.
对我来说 - 我已经在on change event
里面写了一个函数。移动它来$(document).ready(function () {});
解决我的情况。
回答by Scott Reed
Try debugging the code in Firebug rather than alerting. It may be loosing the focus and returning it is causing the appearance of two changes when there isn't two happening
尝试在 Firebug 中调试代码而不是发出警报。它可能会失去焦点并返回它会导致在没有发生两次变化时出现两次变化