EDIT3:更多的人保持downvoting在此之前,请注意,这不是一个重复的问题。为什么复选框总是返回相同的值?
我的工作,需要一个完整的复选框的表一个网站,每一个都需要开/关切换。有时切换一个需要切换其他人,等等。
我没有构建基本代码,但由于它对所有逻辑进行的相当混乱,我决定做一些工作,比如收音机,所以我们只有一个基本功能来完成大部分逻辑工作。
我也有一个功能启用/禁用一些复选框。这一切都很好,没有问题。
但是现在有在玩另一件事是文本输入。如果在其中选择了某个值,则某些复选框会自动打开/关闭并锁定。使用相同的基本功能来执行此操作。但是,当一个复选框获取与此禁用,然后重新启用,下面的函数只保留该复选框返回总是如此:
$('input[type="checkbox"]').change(function() {
// Make checkboxes with names work like radios (radios can't be toggled off)
if ($(this).attr("name")) {
// Save clicked toggle value
var selected_toggle = $(this).attr('value');
// disable all and then toggle clicked one on
if ($('input[value="'+selected_toggle+'"]').prop("checked")) {
console.log("This is checked");
$('input:checkbox[name='+$(this).attr('name')+']').each(function() {
toggle_checkbox($(this).attr('value'), 'disabled');
});
toggle_checkbox(selected_toggle, 'enabled', 'on');
}
// Enable them all back if unchecking
else {
console.log("This is unchecked");
$('input:checkbox[name='+$(this).attr('name')+']').each(function() {
toggle_checkbox($(this).attr('value'), 'enabled');
});
$(this).parent().toggleClass("on");
}
}
else {
// Toggle them on or off
$(this).parent().toggleClass("on");
}
});
最明显的事情是说“锁定时的东西是不同的/解锁该复选框”,但它的所有相同的功能,同样的方式,所有其他的切换得到禁止,这是从toggle_checkbox功能使用:
function toggle_checkbox(toggle, value, on, lock) {
// Disable the toggle completely
if (value == 'disabled') {
$('input[value="'+toggle+'"]').attr('disabled', 'disabled');
$('input[value="'+toggle+'"]').parents('li').addClass('disabled');
$('input[value="'+toggle+'"]').parent().removeClass('on');
$('input[value="'+toggle+'"]').prop('checked', false);
}
// Enable a toggle back
if (value == 'enabled') {
$('input[value="'+toggle+'"]').parents('li').removeClass('disabled');
// Turn it on
if (on == 'on') {
$('input[value="'+toggle+'"]').parent().addClass('on');
$('input[value="'+toggle+'"]').prop('checked', true);
}
// Lock it on or remove disabled
if (lock == 'lock') {
$('input[value="'+toggle+'"]').attr('disabled', 'disabled');
}
else {
$('input[value="'+toggle+'"]').removeAttr('disabled');
}
}
}
当我删除上面的代码名称属性复选框工作正常。然而,尽管所有其他复选框回报选中/取消正确,点击特定非禁用一个的时候,它总是返回“这个被选中。”
我用的是:前(“选中”),改变托没有区别。
编辑:正如评论中所说,使用prop或attr没有什么区别。这是锁定的是被“卡住”的复选框代码:
function set_hint_toggles(type, state) {
if (type == 'apples') { // Set when you select something with apples
toggle_checkbox('pears', 'disabled');
toggle_checkbox('apples', 'enabled', 'on', 'lock');
toggle_checkbox('oranges', 'disabled');
}
if (state == 'off') { // This is set when a reset button is pressed
var toggles_type = $('#hidden-input-with-value').val();
toggle_checkbox(toggles_type, 'enabled'); // This checkbox gets stuck, all others continue working
$('#text-input').removeAttr("disabled"); // Allows to type something again
}
}
EDIT2:试图马文的回答后,我得到了一个性能稍微不同的结果,可以帮助找出什么是错的:
此卡复选框有一个“检查”和“禁用”状态。当点击复位按钮时,禁用被删除,但它仍然被选中(或应该)。当我在复位后用上面的代码尝试点击复选框时,它就像重新检查过一次,然后一直卡住检查。
使用[0] .disabled = false,我可以继续打开和关闭复选框,但没有js被激活,并且每次点击仍然会被选中。
Edit4:当尝试使用
toggle_checkbox(toggles_type, 'disabled');
toggle_checkbox(toggles_type, 'enabled');
在,使复选框的代码会被卡住(set_hint_toggles(关)上述功能),它只有被禁用。之后,这个复选框总是返回false而不是总是true。
当你设置属性时,属性也被设置,当你删除属性时,属性不会被删除,你必须将它设置为false,这就是为什么你应该使用'prop' – adeneo
然而,为什么它只发生在一种情况下,而不是全部?有什么不同? – Forty
@adeneo - 修好了!你可以把它作为答案张贴,以便我可以标记解决?为我学习正确的js还有很长的路要走。 – Forty