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

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


当前回答

我需要检查一个检查箱的检查财产,并使用jQuery进行检查财产的操作。

X -

1) 运行上载以获得检查箱值,如果年龄检查箱被检查,那么我需要显示一个文本框进入年龄,否则隐藏文本框。

2)如果年龄检查框被检查,那么我需要显示一个文本框进入年龄,否则用检查框的点击事件隐藏文本框。

因此,代码不会默认返回虚假:

尝试下列:

<html>
        <head>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        </head>
        <body>
            <h1>Jquery Demo</h1>
            <input type="checkbox" name="isAge" checked id="isAge"> isAge <br/>
            <div id="Age" style="display:none">
              <label>Enter your age</label>
              <input type="number" name="age">
            </div>
            <script type="text/javascript">
            if(document.getElementById('isAge').checked) {
                $('#Age').show();
            } else {
                $('#Age').hide();
            }   
            $('#isAge').click(function() {
                if(document.getElementById('isAge').checked) {
                    $('#Age').show();
                } else {
                    $('#Age').hide();
                }
            }); 
            </script>
        </body>
    </html>

此分類上一篇: https://jsfiddle.net/sedhal/0hygLtrz/7/

其他回答

这个代码会帮助你

$('#isAgeSelected').click(function(){
   console.log(this.checked);
   if(this.checked == true) {
        $("#txtAge").show();
    } else {
       $("#txtAge").hide();
   }
});

我进入了相同的问题,我有一个ASP.NET检查箱。

<asp:CheckBox ID="chkBox1" CssClass='cssChkBox1' runat="server" />

在 jQuery 代码中,我使用下面的选择器来检查检查检查箱是否已被检查,并且它似乎像一个魅力一样工作。

if ($("'.cssChkBox1 input[type=checkbox]'").is(':checked'))
{ ... } else { ... }

我相信你也可以使用 ID 而不是 CssClass,

if ($("'#cssChkBox1 input[type=checkbox]'").is(':checked'))
{ ... } else { ... }

我希望这能帮助你。

输入类型="checkbox"的检查属性与默认检查属性进行地图,而不是检查属性。

因此,当在一个页面上做点什么时,当一个检查箱在未检查的情况下进行检查时,请使用 prop() 方法。

使用 attr() 或 getAttribute(在纯粹的 JavaScript 中)在这些情况下不是做事的正确方式。

如果元素是相关的检查箱,那么做这样的事情来征收值:

elem.checked

$(elem).prop('checked')

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

$(document).ready(function(){

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

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

   });

});

jQuery 1.6 以上

$('#isAgeSelected').prop('checked')

jQuery 1.5 或更低

$('#isAgeSelected').attr('checked')

任何 jQuery 版本

// Assuming an event handler on a checkbox
if (this.checked)

所有的信用都交给Xian。