我需要检查一个检查箱的检查财产,并使用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来展示这种方法。

其他回答

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

$('#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(“检查”),它只会返回真实和虚假。

使用:

<input type="checkbox" id="abc" value="UDB">UDB
<input type="checkbox" id="abc" value="Prasad">Prasad
$('input#abc').click(function(){
  if($(this).is(':checked'))
  {
    var checkedOne=$(this).val()
    alert(checkedOne);

    // Do some other action
  }
})

这可能有助于您希望所需的操作只能在您删除检查时检查框时进行。

这对我来说工作:

/* isAgeSelected being id for checkbox */

$("#isAgeSelected").click(function(){
  $(this).is(':checked') ? $("#txtAge").show() : $("#txtAge").hide();
});
if($("#checkkBoxId").is(':checked')){
  alert("Checked=true");
}

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

$(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="