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

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


当前回答

$(document).ready(function() {    
    $('#agecheckbox').click(function() {
        if($(this).is(":checked"))
        {
            $('#agetextbox').show();
        } else {
            $('#agetextbox').hide();
        }
    });
});

其他回答

相同: 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');
    };
});

下面是一个例子,包括启动显示/隐藏,以匹配页面加载时检查箱的状态;考虑到Firefox在更新页面时记得检查箱的状态,但不会记得显示/隐藏的元素的状态。

$(function() {
    // initialise visibility when page is loaded
    $('tr.invoiceItemRow').toggle($('#showInvoiceItems').attr('checked'));
    // attach click handler to checkbox
    $('#showInvoiceItems').click(function(){ $('tr.invoiceItemRow').toggle(this.checked);})
});

(与此问题的其他答案的帮助)

我使用谷歌的CDN,还有很多CDN可以选择。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

如果当前点击的检查框被检查,则将关闭所有其他,并启用当前的,如果当前的未检查,则将重新启用所有检查框。

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

        var checkbox_selector = '.mycheck input[type=checkbox]';

        $(checkbox_selector).click(function() {
            if ($($(this)).is(':checked')) {

                // Disable all checkboxes
                $(checkbox_selector).attr('disabled', 'disabled');

                // Enable current one
                $($(this)).removeAttr('disabled');
            }
            else {
                // If unchecked open all checkbox
                $(checkbox_selector).removeAttr('disabled');
            }
        });
    });
</script>

简单的测试方式

<form method="post" action="">
    <div class="mycheck">
        <input type="checkbox" value="1" /> Television
        <input type="checkbox" value="2" /> Computer
        <input type="checkbox" value="3" /> Laptop
        <input type="checkbox" value="4" /> Camera
        <input type="checkbox" value="5" /> Music Systems
    </div>
</form>

输出屏幕:

此分類上一篇

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

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

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

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

elem.checked

$(elem).prop('checked')

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

检查箱 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>