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

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


当前回答

下面是一个例子,包括启动显示/隐藏,以匹配页面加载时检查箱的状态;考虑到Firefox在更新页面时记得检查箱的状态,但不会记得显示/隐藏的元素的状态。

$(function() {
    // initialise visibility when page is loaded
    $('tr.invoiceItemRow').toggle($('#showInvoiceItems').attr('checked'));
    // attach click handler to checkbox
    $('#showInvoiceItems').click(function(){ $('tr.invoiceItemRow').toggle(this.checked);})
});

(与此问题的其他答案的帮助)

其他回答

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

$("#mycheckbox").change(function() {
    var value = $(this).prop("checked") ? 'true' : 'false';                     
    alert(value);
});

这个功能是替代和稳定的:

$('#isAgeSelected').context.checked
(return True/False)

例子:

if($('#isAgeSelected').context.checked){ //if Checkbox is checked then bla bla..
    /*.....*/
}else{
    /*.....*/
}

$(document).on(“点击”,“#isAgeSelected”,函数(){如果($(这)。prop(“检查”) ==真实){ $(“#txtAge”).show(); } 否则如果($(这)。prop(“检查”) ==虚假){ $(“#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"> <input type="

由于 jQuery 1.6, jQuery.attr() 的行为已经改变,用户被鼓励不要使用它来恢复一个元素的检查状态。

$("#txtAge").toggle(
    $("#isAgeSelected").prop("checked") // For checked attribute it returns true/false;
                                        // Return value changes with checkbox state
);

另外两种可能性是:

$("#txtAge").get(0).checked
$("#txtAge").is(":checked")
$('#chk').change(function() { 
    (this.checked)? alert('true') : alert('false');
});



($('#chk')[0].checked)? alert('true') : alert('false');