启用和禁用文本框jquery

时间:2011-09-25 20:49:07

标签: jquery html

我已经使用jQuery启用了多个文本框。但是如果没有选择单选按钮,我不确定如何重新启用它们。我必须对我当前的代码进行哪些修改?

<script type="text/javascript">

$(document).ready(function() {      

    $(":radio[id^=check]").click(function() {
        var textId = "#" + this.id.replace("check", "text");
        $(textId).removeAttr("disabled", "disabled").css('background-color', '#99FF66')
    });

//alert("test");

});
</script>

4 个答案:

答案 0 :(得分:2)

$(textId).removeAttr("disabled", "disabled")替换为$(textId).removeAttr("disabled")

如果您阅读文档很明显,removeAttr不会带两个参数

答案 1 :(得分:1)

您可以使用单个处理程序同时执行这两个操作 - 在未选中单选按钮时禁用,在启用时启用。使用@ AlienWebGuy观察您可以使用已检查的测试来获取属性的值,这可以通过以下方式完成:

$(":checkbox[id^=check]").click(function() {
    $("#" + this.id.replace("check", "text"))
        .prop("disabled", $(this).filter(':checked').length == 0);
});

加上这个CSS

<style>
   input:disabled {
       background-color: #99ff66;
   }
</style>

答案 2 :(得分:0)

您想要操纵属性,而不是属性:

$(textId).prop('disabled',true);$(textId).prop('disabled',false');

答案 3 :(得分:0)

我能够以这种方式完成任务。事实上,我的文本框和单选按钮上有id。那就是单选按钮id =“check1”textbox id =“text1”,单选按钮id =“check2”textbox id =“text2”。然后在我的拒绝id我有单选按钮id =“checker1”和textbox id =“text1”等。

因此,根据选择它将启用它并将其变为绿色,或禁用并将其变为红色。

这是代码。

<script type="text/javascript">

$(document).ready(function() {

    //Goes through each and if click adds css and removes disable
    $(":radio[id^=check]").click(function() {
        var textId = "#" + this.id.replace("check", "text");
        $(textId).removeAttr("disabled").css('background-color', '#99FF66').css('color', '#000')
        });

    //Goes through each and if checked adds disabled and turns red 
    $(":radio[id^=checker]").click(function() {
        var textId = "#" + this.id.replace("checker", "text");
        $(textId).attr("disabled", "disabled").css('background-color', '#FF0033').css('color', '#FFFFFF')
    });

//alert("test");

});
</script>
相关问题