Disabling a control based on checkbox state?
I want to disable the referenced control in the HTML and javascript below, yet it's not doing it. Seems simple enough. What could I be missing?
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<script language="javascript" type="text/javascript">
function Disable(isDisable, controlID) {
var objControl = document.getElementById(controlID);
if (objControl != null) {
objControl.disabled = isDisable;
}
}
</script>
</head>
<body>
<form name="form1" id="form1">
<input name="date?4" type="text" value="1/1/2011 12:00:00 AM" id="date?4" runat="server" style="b开发者_JAVA百科order-color:Black;font-family:Arial;width:300px;"
/><input type="checkbox" style="font-family: Arial" onclick="Disable(this.checked, "date?4" );" >Disable
</form></body>
</html>
Your onclick is badly formatted:
onclick="Disable(this.checked, "date?4" );"
Use this instead:
onclick="Disable(this.checked, 'date?4' );"
If you use double quotes ("
) as your attribute value delimiters, you can't use them inside the value without escaping them. You should use single quotes ('
) inside.
Your string is broken in
onclick="Disable(this.checked, "date?4" );"
try with:
onclick="Disable(this.checked, 'date?4' );"
Additionally, according to w3schools, the '?' character is not valid in HTML ids.
You have double quotes in your onclick, so it's breaking the parsing. Change:
onclick="Disable(this.checked, "date?4" );"
to
onclick="Disable(this.checked, 'date?4' );"
I also had to drop the '?' in your id to get it to work in jsFiddle. Think some browsers might not like that.
You are using " within a " enclosed attribute.
精彩评论