使用单个复选框启用多个文本框

时间:2013-04-23 09:56:26

标签: javascript

我正在创建一个复选框以启用多个文本框。如果选中,则默认启用和禁用。

Javascript代码:

$(document).ready(function(){
    $('#sccb').click(function(){
        if (this.checked) {
            $('#cns').removeAttr("disabled");
            $('#cns2').removeAttr("disabled");
            $('#cns3').removeAttr("disabled");
        } else {
            $("#cns").attr("disabled", true);
            $("#cns2").attr("disabled", true);
            $("#cns3").attr("disabled", true);
        }
    });
});

HTML code:

<input type="checkbox" id="sccb" name="science" value="science">
<input type="text" id="cns" name="coornamescience" disabled="disabled" size="30" />
<input type="text" id="cns2" name="coornamescience" disabled="disabled" size="30" />
<input type="text" id="cns3" name="coornamescience" disabled="disabled" size="30" />

它适用于jsfiddle,但它在.html文件中不起作用,请帮忙。

1 个答案:

答案 0 :(得分:2)

你忘记添加jquery了吗?

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#sccb').click(function(){
        if (this.checked) {
            $('#cns').removeAttr("disabled");
            $('#cns2').removeAttr("disabled");
            $('#cns3').removeAttr("disabled");
        } else {
            $("#cns").attr("disabled", true);
            $("#cns2").attr("disabled", true);
            $("#cns3").attr("disabled", true);
        }
    });
});
</script>
</head>
<body>
    <form ...>
         Your form here
    </form>
</body>
</html>
相关问题