繁体   English   中英

删除复选框上的选中属性

[英]Remove checked property on a checkbox

我有这个表格:

<div class="form-group">
    <label class="checkbox">
        <input type="checkbox" name="item1" value="true" checked> Item 1
    </label>
</div>
<div class="form-group">
    <label class="checkbox">
        <input type="checkbox" name="item2" value="true"> Item 1
    </label>
</div>

而这个JS:

$.(".checkbox input[type='checkbox']").each(function() {
    if ($.(this).is(":checked")) {
        $.(this).closest(".checkbox").addClass("checked");
        $.(this).closest(".radio").addClass("checked");
    }
});
$.(".checkbox input[type='checkbox']").bind("change", function() {
    if ($.(this).is(":checked")) {
        $.(this).closest(".checkbox").addClass("checked");
    } else {
        $.(this).closest(".checkbox").removeClass("checked");
    }
});

http://jsfiddle.net/jze3fwdp/


我如何更改我的JS代码以删除未选中复选框中的checked属性?

实际上,类已更改,但检查值保持不变。

谢谢。


我尝试没有成功:

$.(this).prop('checked', true);

$.(this).closest(".checkbox > input:checkbox").prop('checked', false);

使用$.(this).closest(".checkbox").attr("checked", ''); 对于远程属性,检查和$.(this).closest(".checkbox").attr("checked", 'checked'); 添加它。

当您使用addClass它会添加一个CSS类,您想要更改checked的属性而不触摸CSS类。

编辑:

$(".checkbox input[type='checkbox']").each(function() {
    updateState($(this));
});

$(".checkbox input[type='checkbox']").on("change", function() {
    updateState($(this));
});

function updateState($input){
    if ($input.is(":checked")) {
        $input.closest(".checkbox").addClass("checked");
        $input.closest(".radio").addClass("checked");
    }
    else{
        $input.removeAttr('checked');
        $input.closest(".checkbox").removeClass("checked");
        $input.closest(".radio").removeClass("checked");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM