无法控制复选框选中状态-jquery
我无法使用此onClick事件来控制复选框的选中状态。。有什么想法吗无法控制复选框选中状态-jquery,jquery,Jquery,我无法使用此onClick事件来控制复选框的选中状态。。有什么想法吗 init: function () { $('.billing input[type="checkbox"]').bind('click', function (e) { e.preventDefault(); iKeyless.page.toggleShippingForm($(e.currentTarget));
init: function () {
$('.billing input[type="checkbox"]').bind('click', function (e) {
e.preventDefault();
iKeyless.page.toggleShippingForm($(e.currentTarget));
});
},
toggleShippingForm: function (el) {
if (el.attr('checked')) {
$('.shipping').parents('.yui-u').addClass('hide');
el.attr('checked', false);
} else {
$('.shipping').parents('.yui-u').removeClass('hide');
el.attr('checked', true);
}
}
它加载时复选框已选中,并且无论我做什么都保持选中状态…它应该是
此
传入以引用
元素…虽然您不需要jQuery对象,但这里有一个工作版本:
init: function () {
$('.billing input[type="checkbox"]').bind('click', function (e) {
e.preventDefault();
iKeyless.page.toggleShippingForm($(this));
});
},
toggleShippingForm: function (el) {
if (el.attr('checked')) {
$('.shipping').parents('.yui-u').addClass('hide');
el.attr('checked', false);
} else {
$('.shipping').parents('.yui-u').removeClass('hide');
el.attr('checked', true);
}
}
但是,由于是一个DOM属性,您可以这样做(无需转换为jQuery对象):
元素正在更改它的。已选中状态…只需在更改事件中使用它即可。如果需要,将.toggleClass('hide',el.checked)
更改为.toggleClass('hide',!el.checked)
,具体取决于其显示方式
init: function () {
$('.billing input[type="checkbox"]').change(function (e) {
e.preventDefault();
iKeyless.page.toggleShippingForm(this);
});
},
toggleShippingForm: function (el) {
$('.shipping').parents('.yui-u').toggleClass('hide', el.checked);
}