如何获得单击不确定复选框以'checked'状态



我试图遵循这个线程,建议一个解决方案来检查不确定状态。

我正在使用ASP。在不确定状态下单击复选框似乎会取消选中它,而不会触发任何事件(底层复选框实际上是"未选中的")。当我单击处于不确定状态的复选框(更喜欢这种逻辑)时,我正在考虑让Javascript为我检查它,这应该会触发我的ASP。净事件).

$(document).ready(function () {
    $(".UndefinedCheckboxState :checkbox").prop("indeterminate", true);
    $(":checkbox").click(function () {
        if ($(this).prop("indeterminate")) {
            $(this).prop("indeterminate", false);
            $(this).prop("checked", true);
        }
    });
});

UPDATE:点击事件工作正常,但if中的条件永远不会为真!我一直点击不确定状态的复选框

如果单击的复选框具有不确定状态,则通过单击事件中的第一条指令,该属性已经消失。

我已经替换了我的逻辑来测试一些还没有消失的东西:

<script type="text/javascript">
    $(document).ready(function() {
        $(".UndefinedCheckboxState :checkbox").prop("indeterminate", true);
        $(":checkbox").click(function () {
            if ($(this).closest("span").hasClass("UndefinedCheckboxState")) {
                $(this).prop("indeterminate", false);
                $(this).prop("checked", true);
            }
        });
    });
</script>

扩展@adeneo的答案,试试这个:

$(document).ready(function () {
    $(".UndefinedCheckboxState input[type='checkbox']").prop("indeterminate", true);
    $("input[type='checkbox']").on('click', function () {
        console.log('changed');
        console.log('current value: '+$(this).prop('checked') );  
    });
});

View on Fiddle

只用is()

$(document).ready(function () {
    $(".UndefinedCheckboxState :checkbox").prop("indeterminate", true);
    $(":checkbox").click(function () {
        if ($(this).is(":indeterminate")) {
            $(this).prop("indeterminate", false);
        }
        $(this).prop("checked", true);
    });
});

最新更新