为什么我的 checkbox.change 不起作用(jquery)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25721031/
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
why is my checkbox.change not working (jquery)?
提问by Dird
I've done similar things before, click a checkbox then do something (.parent() etc) but for some reason it's not registering this time..can anyone see why?
我以前做过类似的事情,点击一个复选框然后做一些事情(.parent() 等)但由于某种原因它这次没有注册..谁能明白为什么?
$.each( email_list, function( key, value ) {
$("#email_span").after("<tr><td><input type='checkbox' name='member' checked='checked' onclick='check_change(this);' /></td><td>" + value.name + "</td><td>" + value.email + "</td></tr>");
$("#emails").prepend(value.email + ";");
});
$('.member').change(function() {
alert ("FECK");
if(this.checked) {
}
});
It is all within the $(document).ready block. I've got it working with a function call but was curious why the jquery .change wasn't working?
它都在 $(document).ready 块中。我已经让它与函数调用一起工作,但很好奇为什么 jquery .change 不起作用?
回答by Irvin Dominin
For dynamically created element you have to use event delegation
, if you want to select by an attribute you can use an attribute equals selector
.
对于动态创建的元素,您必须使用event delegation
,如果您想通过属性进行选择,您可以使用attribute equals selector
.
Code:
代码:
$(document).on("change", "input[name='member']", function () {
alert("FECK");
if (this.checked) {}
});
回答by Harry S.
If the element has the 'change' event bound and if you need to fire the 'change' event after changing the checkbox property you can use the alt workaround below:
如果元素绑定了 'change' 事件,并且需要在更改复选框属性后触发 'change' 事件,则可以使用下面的 alt 解决方法:
If you want to check:
如果你想检查:
$('#MyCheckbox input:checkbox').prop('checked', false).click();
If you want to uncheck:
如果您想取消选中:
$('#MyCheckbox input:checkbox').prop('checked', true).click();
Summary
概括
it's using a reverse process. for the first one it will make sure the checkbox property is NOT checked, then it will trigger the click which will change the checkbox property to 'checked' then it will fire the 'change' event - if it's bound.
它使用反向过程。对于第一个,它将确保未选中复选框属性,然后它将触发单击,将复选框属性更改为“已选中”,然后将触发“更改”事件 - 如果它已绑定。
回答by antyrat
You are using classselector, but you don't have class called .member
. You need to use attribute equal electorinstead:
您正在使用类选择器,但您没有名为.member
. 您需要使用属性相等的选举人:
$('input[name=member]').change...
回答by Kartikeya Khosla
Give your checkboxes
a class say 'check'
and then try event delegation as shown below :-
给你checkboxes
一个班级说'check'
,然后尝试事件委托,如下所示:-
$(document).on('change','.check',function() {
alert ("FECK");
if(this.checked) {
alert ("checked");
}
});
OR
或者
$(document).on('change','input[name="member"]',function() {
alert ("FECK");
if(this.checked) {
alert ("checked");
}
});
回答by vikrant singh
Delegate the change
event
委托change
活动
$("tr").on("change","input[name='member']",function(){
alert ("FECK");
if(this.checked) {
//do other stuff
}
})
回答by Martin M
You are binding the 'change' listener inside your $.each loop.. Maybe just make the change listener more specific like:
您正在 $.each 循环中绑定“更改”侦听器。也许只是使更改侦听器更具体,例如:
$('#email_span').on('change', '.member', function(){
console.log('you hit it'); if($(this).prop('checked')){
console.log('it\'s checked');
}
});
回答by Arash.Zandi
You shoud use this way :
你应该使用这种方式:
$('body').on('change', 'input[type=checkbox]',function (e) {
//what ever you wanna do
});
(Because google search shows this page for another issues It make sense to explain more)
(因为谷歌搜索显示这个页面是为了另一个问题 解释更多是有意义的)
And If you want to checkand uncheckprogrammatically with raising changeevent you should add .change()at the end of lines like these :
如果您想通过引发更改事件以编程方式选中和 取消选中,则应在如下行的末尾添加.change():
$('checkboxGroups').prop('checked', true).change();
$('checkboxGroups').prop('checked', false).change();
回答by Utk
Try adding such scripts inside:
尝试在里面添加这样的脚本:
$(document).ready(function () {});
function.
功能。