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

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


当前回答

上面的答案不是为了我,但这就是:

<script type="text/javascript">
    $(document).ready(function(){

        $("#li_13").click(function(){
            if($("#agree").attr('checked')){
                $("#saveForm").fadeIn();
            }
            else
            {
                $("#saveForm").fadeOut();
            }
        });
    });
</script>

基本上,当点击 #li_13 时,它检查是否符合 # 元素(即检查框) 通过使用.attr(“检查”) 函数进行检查. 如果是 fadeIn #saveForm 元素,如果不是 fadeOut 元素 saveForm。

其他回答

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

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

事实上,我更喜欢改变事件。

$('#isAgeSelected').change(function() {
    $("#txtAge").toggle(this.checked);
});

饰 Fiddle

那么,这个解决方案呢?

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

有很多方法可以检查一个检查箱是否被检查:

如何使用 jQuery 查看

if (elem.checked)
if ($(elem).prop("checked"))
if ($(elem).is(":checked"))
if ($(elem).attr('checked'))

查看例子或文档:

http://api.jquery.com/attr/ http://api.jquery.com/prop/

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

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

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