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

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


当前回答

你可以用两种方式尝试跟踪:

var getVal=$('#isAgeSelected').is(":checked"); // jQuery

var getVal=document.getElementById("isAgeSelected").checked //JavaScript

if (getVal==true) {
 $("#txtAge").show();  // checked
} else {
 $("#txtAge").hide();  // unchecked
}

其他回答

使用:

$(this).toggle($("input:checkbox", $(this))[0].checked);

当您选择不背景时,请记住您需要 [0] 才能访问检查框。

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

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

相同: 0 / 1 或其他

<input type="checkbox" id="nolunch" />
<input id="checklunch />"

    $('#nolunch').change(function () {
    if ($(this).is(':checked')) {
        $('#checklunch').val('1');
    };
    if ($(this).is(':checked') == false) {
        $('#checklunch').val('0');
    };
});

我有相同的问题,没有一个发布的解决方案似乎工作,然后我发现这是因为ASP.NET将检查箱控制作为一个SPAN与INPUT内部,所以检查箱 ID实际上是一个SPAN的ID,而不是一个INPUT,所以你应该使用:

$('#isAgeSelected input')

而不是

$('#isAgeSelected')

然后,上面列出的所有方法都应该工作。

试试这,

$('#isAgeSelected').click(function() {
    if(this.checked){
        $("#txtAge").show();
    } else{
        $("#txtAge").hide();
    } 
});