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

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


当前回答

使用:

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

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

其他回答

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

使用:

<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
  }
})

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

这是一个不同的方法来做同样的事情:

$(document).ready(函数(){ $(#isAgeSelected').click(函数() { // $(#txtAge").toggle(this.checked); // 使用纯 CSS 选择器如果($(this.checked)) { alert('on check 1'); }; // 使用 jQuery's is() 方法如果($(this).is(':checked')) { alert('on checked 2'); }; // 使用 jQue

我正在使用它,它工作得很好:

$("#checkkBoxId").attr("checked") ? alert("Checked") : alert("Unchecked");

注意:如果检查箱被检查,它将返回真实,否则不确定,所以最好检查“真实”值。

如果您的 HTML 标签是:

<input type="checkbox"  />

特斯拉使用:

$(element).attr("checked"); // Will give you undefined as initial value of checkbox is not set

如果使用POP:

$(element).prop("checked"); // Will give you false whether or not initial value is set

2)如果您的HTML标签是:

 <input type="checkbox"  checked="checked" />// May be like this also  checked="true"

特斯拉使用:

$(element).attr("checked") // Will return checked whether it is checked="true"

Prop 使用:

$(element).prop("checked") // Will return true whether checked="checked"