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

// This is how you get them
var output = "";
var checkedBoxes = $("DivCheckBoxesAreIn").children("input:checked");
if(checkedBoxes.length <= 0) {
    alert('Please select check boxes');
    return false;
};

// And this is how you use them:
checkedBoxes.each(function() {
    output +=  this.value + ", ";
};

打印“输出”将为您提供您价值的单独列表。

其他回答

这对我工作:

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

在哪里是AgeSelected是控制的ID。

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

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

使用点击事件处理器对检查箱属性是不可靠的,因为检查的属性在执行事件处理器本身时可能会发生变化!

理想情况下,您希望将代码放入变更事件交易器中,因为它每当检查箱的值发生变化时都会被释放(无论它是如何进行的)。

$('#isAgeSelected').bind('change', function () {

   if ($(this).is(':checked'))
     $("#txtAge").show();
   else
     $("#txtAge").hide();
});

我使用谷歌的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>

输出屏幕:

此分類上一篇

如果您的 HTML 标签是:

<input type="checkbox"  />

特斯拉使用:

$(element).attr("checked"); // Will give you undefined as initial value of checkbox is not set

如果使用POP:

$(element).prop("checked"); // Will give you false whether or not initial value is set

2)如果您的HTML标签是:

 <input type="checkbox"  checked="checked" />// May be like this also  checked="true"

特斯拉使用:

$(element).attr("checked") // Will return checked whether it is checked="true"

Prop 使用:

$(element).prop("checked") // Will return true whether checked="checked"

我在Firefox 9.0.1中确认了下列工作,以捕捉检查箱邮件的状态变更:

$("#mycheckbox").change(function() {
    var value = $(this).prop("checked") ? 'true' : 'false';                     
    alert(value);
});