无法在jQuery中取消选中

kko*_*sky 1 jquery

我有一个复选框设置,应该添加和删除带有传单的图层.现在我无法取消选中复选框.

HTML:

<form action="">
    <input type="checkbox" id="pointCheck" value="true">Points &nbsp;
    <input type="checkbox" id="lineCheck">Lines &nbsp;
    <input type="checkbox" id="polygonCheck">Polygons &nbsp;
</form>
Run Code Online (Sandbox Code Playgroud)

JavaScript的:

$(":checkbox").click(function(){
    if($("#pointCheck").prop('checked', true)){
        $.getJSON("../data/point.json").done(function(data){
            addPoints(data);
            console.log("hello");
        });
    }
    else if($("#pointCheck").prop('checked', false)){
        console.log("remove");
    }
})
Run Code Online (Sandbox Code Playgroud)

Moh*_*sef 5

我在1小时前回答了那个问题

if($("#pointCheck").is(':checked')){
    alert('button checked');
}else{
   alert('button unchecked');
}
Run Code Online (Sandbox Code Playgroud)

您可以使用下一个代码来设置attr选中(以使#pointCheck被选中并且false被取消选中),并且对于您的信息,您可以使用prop也可以选择和禁用..同样的方式..

$("#pointCheck").prop('checked', true)
Run Code Online (Sandbox Code Playgroud)

正如@TheGrandPackard在评论手册参考中提到的那样