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

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


当前回答

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

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

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

其他回答

相同: 0 / 1 或其他

<input type="checkbox" id="nolunch" />
<input id="checklunch />"

    $('#nolunch').change(function () {
    if ($(this).is(':checked')) {
        $('#checklunch').val('1');
    };
    if ($(this).is(':checked') == false) {
        $('#checklunch').val('0');
    };
});

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

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

在纯粹的JS检查箱状态中,更容易阅读

isAgeSelected.checked

函数检查() { txtAge.style.display= isAgeSelected.checked? 'block':'none'; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 年龄 <input type="checkbox" id="isAgeSelected"/> <button onclick="check()"> Check</button> <div id="txtAge" style="display:none"> 年龄 已选择 </div>

那么,这个解决方案呢?

$("#txtAge")[
    $("#isAgeSelected").is(':checked') ?
    'show' :
    'hide'
]();

使用此:

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

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