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

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


当前回答

我相信你可以这样做:

if ($('#isAgeSelected :checked').size() > 0)
{
    $("#txtAge").show(); 
} else { 
    $("#txtAge").hide();
}

其他回答

使用 jQuery > 1.6

<input type="checkbox" value="1" name="checkMeOut" id="checkMeOut" checked="checked" />

// traditional attr
$('#checkMeOut').attr('checked'); // "checked"
// new property method
$('#checkMeOut').prop('checked'); // true

使用新财产方法:

if($('#checkMeOut').prop('checked')) {
    // something when checked
} else {
    // something else when not
}

使用此:

if ($('input[name="salary_in.Basic"]:checked').length > 0)

长度超过零,如果检查箱被检查。

使用点击事件处理器对检查箱属性是不可靠的,因为检查的属性在执行事件处理器本身时可能会发生变化!

理想情况下,您希望将代码放入变更事件交易器中,因为它每当检查箱的值发生变化时都会被释放(无论它是如何进行的)。

$('#isAgeSelected').bind('change', function () {

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

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

如果您需要使用 CSS 类作为 jQuery 选择器,您可以执行以下操作:

$(document).ready(function () {
        $('.myOptionCheckbox').change(function () {            
            if ($(this).prop('checked') == true) {
                console.log("checked");           
            }
            else {
                console.log("unchecked");                
            }
        });
    });

它也适用于检查箱和无线电盒。