我需要检查一个检查箱的检查财产,并使用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>

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


当前回答

这对我工作:

$get("isAgeSelected ").checked == true

在哪里是AgeSelected是控制的ID。

此外, @karim79的答案工作得很好,我不确定我在测试时错过了什么。

注意,这是答案使用Microsoft Ajax,而不是jQuery

其他回答

我在Firefox 9.0.1中确认了下列工作,以捕捉检查箱邮件的状态变更:

$("#mycheckbox").change(function() {
    var value = $(this).prop("checked") ? 'true' : 'false';                     
    alert(value);
});
if( undefined == $('#isAgeSelected').attr('checked') ) {
    $("#txtAge").hide();
} else {
    $("#txtAge").show();
}

塞特:

$("#chkmyElement")[0].checked = true;

吉特:

if($("#chkmyElement")[0].checked) {
   alert("enabled");
} else {
   alert("disabled");
}

有很多方法可以检查一个检查箱是否被检查:

如何使用 jQuery 查看

if (elem.checked)
if ($(elem).prop("checked"))
if ($(elem).is(":checked"))
if ($(elem).attr('checked'))

查看例子或文档:

http://api.jquery.com/attr/ http://api.jquery.com/prop/

这对我工作:

$get("isAgeSelected ").checked == true

在哪里是AgeSelected是控制的ID。

此外, @karim79的答案工作得很好,我不确定我在测试时错过了什么。

注意,这是答案使用Microsoft Ajax,而不是jQuery