-1

I'm creating a checkbox to enable multiple textbox. If it is checked, it will enable and disabled by default.

Javascript code :

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

It works on jsfiddle, but it doesn't work in a .html file, please help.

4

1 回答 1

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>
于 2013-04-23T10:00:42.283 回答