jQuery Html checkBox onchange 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4942437/
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
Html checkBox onchange not working
提问by isxaker
<input class="jProblemClass" id="Checkbox{%= ID %}" type="checkbox" onchange="problemPicker.onChangeProblemCheckBox('{%=ID %}');"/>
After first check or uncheck nothing happens. Afetr second click, call my function problemPicker.onChangeProblemCheckBox
, but i get ID first check. Why? Can help me anybody?
第一次选中或取消选中后什么也没有发生。Afetr 第二次点击,调用我的函数problemPicker.onChangeProblemCheckBox
,但我首先检查 ID。为什么?任何人都可以帮助我吗?
onChangeProblemCheckBox: function(id) {
if ($('#CheckBox' + id).attr("checked") == true) {
problemPicker.addToCheckProblems(id);
var checkboxes = $('.jProblemClass:checked');
if ((checkboxes.length > general.limit) && (general.limit != 0)) {
alert('The limit of ' + general.limit + ' problems exceeded. Please deselect ' + (checkboxes.length - general.limit).toString() + '.');
}
} else {
problemPicker.delFromCheckProblems(id);
}
},
回答by Tami
Bro use onclick event instead onchange..Reason... According to the Javascript references I've read, onchange fires after the focus is lost. Unless you click somewhere else, the focus is not lost in IE. The other browsers must not be waiting for the focus to be lost before firing onchange - which suggest they are not following the spec correctly (despite it making more sense to fire at that point). How about using onclick and onkeydown/onkeyup instead (by using both onclick and onkeyup/onkeydown you cover both mouse and keyboard setting of the checkbox). REFERENCE : http://www.winvistatips.com/re-onchange-event-checkbox-not-working-properly-t311364.html
兄弟使用 onclick 事件而不是 onchange ..原因...根据我读过的 Javascript 参考,onchange 在失去焦点后触发。除非您单击其他位置,否则焦点不会在 IE 中丢失。其他浏览器在触发 onchange 之前一定不能等待焦点丢失 - 这表明它们没有正确遵循规范(尽管此时触发更有意义)。如何使用 onclick 和 onkeydown/onkeyup 代替(通过同时使用 onclick 和 onkeyup/onkeydown,您可以覆盖复选框的鼠标和键盘设置)。参考:http: //www.winvistatips.com/re-onchange-event-checkbox-not-working-properly-t311364.html
回答by shybovycha
I think, the problem goes from here: $('#CheckBox' + id).attr("checked") == true
. Accordingly to HTML specs, checked
should be set to "checked"
when this event fires. So, try using something like $('#CheckBox' + id).attr("checked") == "checked"
or even $('#CheckBox' + id).attr("checked")
.
我认为,问题从这里开始:$('#CheckBox' + id).attr("checked") == true
. 根据 HTML 规范,当此事件触发checked
时应设置为"checked"
。因此,请尝试使用类似$('#CheckBox' + id).attr("checked") == "checked"
或什至$('#CheckBox' + id).attr("checked")
.
As a second option, i recommend you to use pure jquery to run your routine. For example, if you have <input type="checkbox" id="ac">
checkbox, you can use this jq code to handle your routines:
作为第二个选择,我建议您使用纯 jquery 来运行您的例程。例如,如果您有<input type="checkbox" id="ac">
复选框,则可以使用此 jq 代码来处理您的例程:
$(document).ready(function() {
$("input[type=checkbox]").change(function() {
alert("Am i checked? - " + $(this).attr("checked") + "\nMy ID:" + $(this).attr("id"));
});
});
This case is shown in this demo.
此案例显示在此演示中。
回答by David M?rtensson
Try using if($('#CheckBox' + id + ':checked').length > 0)
instead, that way you led jQuery figure out it the checkbox is checkedor not.
尝试if($('#CheckBox' + id + ':checked').length > 0)
改用,这样你就引导 jQuery 找出复选框是否被选中。
Also if the onChangeProblemCheckBox
uses any internal properties in problemPicker
but does not referens them with full path, ie problemPicker.nnnnn
it will fail as the event will fire in the context of the element, not problemPicker. It would be as if the function was called like this $('#CheckBox' + id).onChangeProblemCheckBox()
.
此外,如果onChangeProblemCheckBox
使用了任何内部属性,problemPicker
但没有使用完整路径引用它们,即problemPicker.nnnnn
它将失败,因为事件将在元素的上下文中触发,而不是问题选择器。就好像函数是这样调用的$('#CheckBox' + id).onChangeProblemCheckBox()
。