2013-04-23 105 views
-1

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

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代码:

<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文件的工作,请大家帮忙。

+3

您需要包括jQuery来使这个工作 – anpsmn 2013-04-23 09:59:31

回答

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> 
+0

我猜的jsfiddle包括自动jQuery的 – ElSinus 2013-04-23 10:03:59

+0

哦,我忘了包括,如果我有一个很长的PHP的形式,哪里我把javascript代码在哪里? @ElSinus – rapide 2013-04-23 10:19:24

+0

这够清楚了吗? – ElSinus 2013-04-23 10:42:32

相关问题