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

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


当前回答

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

其他回答

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

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

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

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

我发现这种方法是有用的;它返回了一系列检查的检查盒,然后你可以使用它们的值(这个解决方案使用jQuery):

// This is how you get them
var output = "";
var checkedBoxes = $("DivCheckBoxesAreIn").children("input:checked");
if(checkedBoxes.length <= 0) {
    alert('Please select check boxes');
    return false;
};

// And this is how you use them:
checkedBoxes.each(function() {
    output +=  this.value + ", ";
};

打印“输出”将为您提供您价值的单独列表。

使用:

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

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

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

检查箱 DOM 元素的检查属性将为您提供检查元素的状态。

考虑到您的现有代码,您可以这样做:

if(document.getElementById('isAgeSelected').checked) {
    $("#txtAge").show();
} else {
    $("#txtAge").hide();
}

但是,有一个更方便的方式来做到这一点,使用Toggle:

$('#isAgeSelected').click(功能() { $("#txtAge").toggle(this.checked); }; <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>