1

我有五个选择加入复选框和一个取消订阅复选框。当我单击选择加入复选框时,如果没有选择加入复选框保持选中状态,则应该自动选中取消订阅复选框。如果我随后选中其中一个选择加入框,则取消订阅复选框应该被自动取消选中。

取消订阅复选框的自动取消选中效果很好。当没有选中任何选择加入复选框时,我无法开始工作的是自动检查取消订阅复选框。

var $jQ = jQuery.noConflict();

$jQ("#Unsubscribed").click(function(event) {
    opt_ins_unchecked = $(":checkbox" ).not('#Unsubscribed').not(':checked').length;
    if (opt_ins_unchecked == 5 && !$jQ("#Unsubscribed").is(':checked')){
        event.preventDefault();

    } else {
            $jQ("input[name=Opt-In_Premium_Content],input[name=Opt-In_Product_Updates],input[name=Opt-In_Industry_Best_Practices],input[name=Opt-In_Events_Webinars],input[name=Opt-In_Education_Training]").attr("checked", false);
    }
});

$jQ("#Opt-In_Premium_Content, #Opt-In_Product_Updates, #Opt-In_Industry_Best_Practices, #Opt-In_Events_Webinars, #Opt-In_Education_Training").click(function() {
    opt_ins_unchecked = $(":checkbox" ).not('#Unsubscribed').not(':checked').length;
    if (opt_ins_unchecked == 5){
        $jQ("#Unsubscribed").attr("checked", true);
    }

    if (opt_ins_unchecked != 0){
        $jQ("#Unsubscribed").attr("checked", false);
    }
});

$jQ("#Unsubscribed").attr("checked", true);当我没有选中选择加入复选框时,为什么线路不运行?当我取消选中所有选择加入复选框时,如何让取消订阅框被选中?

4

3 回答 3

1

使用.prop()而不是.attr()设置检查状态

$jQ("#Unsubscribed").prop("checked", true);

尝试

var $unsubscribed = $jQ("#Unsubscribed");
var $checks = $jQ("#Opt-In_Premium_Content, #Opt-In_Product_Updates, #Opt-In_Industry_Best_Practices, #Opt-In_Events_Webinars, #Opt-In_Education_Training");

$checks.change(function () {
    $unsubscribed.prop("checked", $checks.filter(':checked').length == 0);
});
于 2013-11-04T06:46:21.547 回答
0

jQuery 1.6+ 推荐 prop() 而不是 attr():

使用新.prop()功能:

$jQ('#Unsubscribed').prop('checked', true);
$jQ('#Unsubscribed').prop('checked', false);

jQuery 1.5 及以下您可以使用attr,因为 prop() 不可用。

或者你可以使用任何 jQuery:

$jQ("#Unsubscribed").removeAttr('checked');
于 2013-11-04T06:48:51.327 回答
0

事实证明它不起作用,因为这两个if语句都在评估true

于 2013-11-04T06:58:27.097 回答