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

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


当前回答

虽然您为您的问题提出了一个JavaScript解决方案(在检查时显示一个文本框),但这个问题只能通过CSS解决。

假设你有以下HTML:

<label for="show_textbox">Show Textbox</label>
<input id="show_textbox" type="checkbox" />
<input type="text" />

您可以使用以下 CSS 来实现所需的功能:

 #show_textbox:not(:checked) + input[type=text] {display:none;}

对于其他场景,您可能会考虑适当的CSS选择器。

这里有一个Fiddle来展示这种方法。

其他回答

if($("#checkkBoxId").is(':checked')){
  alert("Checked=true");
}

if($("#checkkBoxId").attr('checked') == true){
  alert("checked=true");
}

你可以使用平坦的JavaScript,如下:

document.getElementById('checkboxOption').addEventListener('click', event => console.log(event.target.checked) ); <label><input type="checkbox" id="checkboxOption">Check Option</label>

$(“#isAgeSelected”).prop(“检查”,真实);

以点击进行检查或未检查的检查框进行操作。

$('#customCheck1').click(功能() { if (this.checked) { console.log('checked'); } 其他 { console.log('un-checked'); } ); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" id="customCheck1">

编辑: 如果(boolean = = 真实)虽然.checked 属性可能会返回其他类型的变量。

最好使用.prop(“检查”),它只会返回真实和虚假。

请尝试下面的代码检查检查箱是否已检查

$(document).ready(function(){

    $("#isAgeSelected").on('change',function(){

    if($("#isAgeSelected").is(':checked'))
        $("#txtAge").show();  // checked
    else{
        $("#txtAge").hide();  // unchecked
    }

   });

});