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

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


当前回答

试试这,

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

其他回答

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

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

我确信这不是某种启示,但我没有在一个例子中看到这一切:

所有检查的检查箱(页面上):

$('input[type=checkbox]:checked')

这对我工作:

$get("isAgeSelected ").checked == true

在哪里是AgeSelected是控制的ID。

此外, @karim79的答案工作得很好,我不确定我在测试时错过了什么。

注意,这是答案使用Microsoft Ajax,而不是jQuery

我正在使用它,它工作得很好:

$("#checkkBoxId").attr("checked") ? alert("Checked") : alert("Unchecked");

注意:如果检查箱被检查,它将返回真实,否则不确定,所以最好检查“真实”值。

自动化

$(document).ready(function()
{
    $('#isAgeSelected').change(function()
    {
        alert( 'value =' + $('#chkSelect').attr('checked') );
    });
});

HTML

<b> <input type="isAgeSelected" id="chkSelect" /> Age Check </b>

<br/><br/>

<input type="button" id="btnCheck" value="check" />

jQuery

$(document).ready(function()
{
    $('#btnCheck').click(function()
    {
        var isChecked = $('#isAgeSelected').attr('checked');

        if (isChecked == 'checked')
            alert('check-box is checked');
        else
            alert('check-box is not checked');
    })
});

艾雅克斯

function check()
{
    if (isAgeSelected())
        alert('check-box is checked');
    else
        alert('check-box is not checked');
}

function isAgeSelected()
{
    return ($get("isAgeSelected").checked == true);
}