使用复选框和 jQuery 禁用/启用元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3801970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:07:41  来源:igfitidea点击:

Disable/enable element with checkbox and jQuery?

jquery

提问by Martin

I have a checkbox and if I tick it I want a textfield to become enabled (disabled as default) and when I untick the the checkbox I want it to become disabled again.

我有一个复选框,如果我勾选它,我希望文本字段被启用(默认禁用),当我取消选中复选框时,我希望它再次被禁用。

I saw here jQuery Checkboxeshow I caan toggle a CSS class and here http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_disable.2Fenable_a_form_element.3Fhow I can switch between enabled and disabled with two buttons. But how do I toggle a textfields disabled/enabled status by tick/untick a checkbox?

我在这里看到了jQuery Checkboxes如何切换 CSS 类,在这里看到了http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_disable.2Fenable_a_form_element.3F如何使用两个按钮在启用和禁用之间切换。但是如何通过勾选/取消勾选复选框来切换文本字段禁用/启用状态?

Thanks in advance.

提前致谢。

回答by Reigel

$(':checkbox').click(function(){
   $('input:text').attr('disabled',!this.checked)
});

crazy demo

疯狂的演示

回答by kennytm

You can attach the change handler on the checkbox, and enable/disable the text field with its checkedproperty.

您可以在复选框上附加更改处理程序,并使用其checked属性启用/禁用文本字段。

$('#theCheckbox').change(function() {
    $('#theTextfield').attr('disabled', this.checked);
});

Example: http://jsbin.com/oludu3/2

示例:http: //jsbin.com/oludu3/2

回答by SamStephens

Here's a page that does what you're looking for - it's pretty minimal but shows what you need

这是一个页面,可以满足您的要求 - 它非常小,但显示了您需要的内容

<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>
        <script>
            $(document).ready(function () {
                $("#testcheckbox").change(function () { 
                    $("#testtextfield").attr("disabled", $(this).attr("checked"));
                });
            });
        </script>
    </head>
    <body>
        <input type="checkbox" id="testcheckbox" />
        <input type="text" id="testtextfield" />
    </body>
</html>

回答by Andrews

@Martin -to delete the text in the textboxes that becomes disabled and untick the checkboxes that becomes disabled - just add this line

@Martin - 删除被禁用的文本框中的文本并取消选中被禁用的复选框 - 只需添加这一行

 $("#TextField_ID_name").val("");

and to uncheck the checkbox, you need to assign ID name to the checkbox and then add this line to the Jquery

并取消选中复选框,您需要为复选框分配 ID 名称,然后将此行添加到 Jquery

$("#chekcbox_ID_name").attr('checked',false)

Hope this helps someone ... though I'm replying to Martins question after 2 years since his posting :-)

希望这对某人有所帮助......尽管我在他发帖 2 年后回答了 Martins 的问题:-)

回答by domenu

Since jQuery 1.6, the .prop()method should be used to set disabled and checked instead of the .attr()method:

从 jQuery 1.6 开始,应该使用.prop()方法来设置禁用和检查,而不是.attr()方法:

$('#theCheckbox').change(function() {
    $('#theTextfield').prop('disabled', this.checked);
});

回答by Iman

show and hide a textbox(#otherSection2) for entering other options when last checkbox in a rendered table(#CheckBoxListList) of asp:CheckBoxList control is selected

显示和隐藏文本框(#otherSection2),用于在选择 asp:CheckBoxList 控件的渲染表(#CheckBoxListList)中的最后一个复选框时输入其他选项

 $("#CheckBoxListList input[type='checkbox']:last").on("click", function () {

                if ($(this).is(":checked")) {
                    $("#otherSection2").show();
                } else {
                    $("#otherSection2").hide().find("input").val(null);

                }
            });