我需要检查一个检查箱的检查财产,并使用jQuery进行检查财产的操作。

例如,如果年龄检查框被检查,那么我需要显示一个文本框进入年龄,否则隐藏文本框。

但下列代码默认返回虚假:

如果($('#isAgeSelected').attr('checked')) { $("#txtAge").show(); } 其他 { $("#txtAge").hide(); } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" id="isAgeSelected"/> <div id="txtAge" style="display:none"> 年龄 已选择 </div>

如何成功查询已检查的房地产?


当前回答

在纯粹的JS检查箱状态中,更容易阅读

isAgeSelected.checked

函数检查() { txtAge.style.display= isAgeSelected.checked? 'block':'none'; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 年龄 <input type="checkbox" id="isAgeSelected"/> <button onclick="check()"> Check</button> <div id="txtAge" style="display:none"> 年龄 已选择 </div>

其他回答

使用此:

if ($('input[name="salary_in.Basic"]:checked').length > 0)

长度超过零,如果检查箱被检查。

您可以尝试检查箱的变更事件,以跟踪:检查状态变更。

$("#isAgeSelected").on('change', function() { if ($("#isAgeSelected").is(':checked')) alert("checked"); other { alert("unchecked"); } }; <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" id="isAgeSelected" /> <div id="txtAge" style="display:none"> 年龄已选择 </div>

对于以前的 jQuery 版本,我不得不使用如下,

$('#change_plan').live('click', function() {
     var checked = $('#change_plan').attr('checked');
     if(checked) {
          //Code       
     }
     else {
          //Code       
     }
});

我正在使用它,它工作得很好:

$("#checkkBoxId").attr("checked") ? alert("Checked") : alert("Unchecked");

注意:如果检查箱被检查,它将返回真实,否则不确定,所以最好检查“真实”值。

我这样做的方式就是:

if ( $("#checkbox:checked").length ) {       
    alert("checkbox is checked");
} else {
    alert("checkbox is not checked");
}