我必须用 javascript 做一件非常基本的事情:表单字段由复选框触发;如果未选中该复选框,则该字段必须禁用且其标签必须为灰色。
编辑:这是从以下 JSP 代码生成的 HTML 代码(当然 c:choose 标记输出第二个条件):
<div class="form">
<form id="myForm" name="myForm" action="settings" method="post">
<table>
<tr>
<td><label for="compatibilityMode">comp. mode</label></td>
<td><input id="compatibilityMode" name="compatibilityMode" onclick="toggle()" name="compatibilityMode" class="checkbox" type="checkbox" value="true"/><input type="hidden" name="_compatibilityMode" value="on"/>
</tr>
<tr>
<td><label for="localbaseuri" id="localbaseurilabel" name="localbaseurilabel" class="labeldisabled">local base URI</label></td>
<td><input id="localbaseuri" name="localApplicationBaseURI" name="localbaseuri" class="text" disabled="disabled" type="text" value="" size="40"/></td>
<td></td>
</tr>
</table>
<div class="form-row">
<input type="submit" value="submit"/>
</div>
</form>
</div>
所以,这是我的代码:
<div class="form">
<form:form id="myForm" name="myForm" method="post" action="settings" commandName="settings">
<table>
<tr>
<td><form:label for="compatibilityMode" path="compatibilityMode">comp. mode</form:label></td>
<td><form:checkbox id="compatibilityMode" class="checkbox" path="compatibilityMode" name="compatibilityMode" onclick="toggle()"/>
</tr>
<tr>
<c:choose>
<c:when test="${settings.compatibilityMode}">
<td><form:label id="localbaseurilabel" name="localbaseurilabel" class="labelenabled" for="localbaseuri" path="localApplicationBaseURI">local base URI</form:label></td>
<td><form:input id ="localbaseuri" class="text" path="localApplicationBaseURI" size="40" name="localbaseuri" disabled="false"/></td>
</c:when>
<c:otherwise>
<td><form:label id="localbaseurilabel" name="localbaseurilabel" class="labeldisabled" for="localbaseuri" path="localApplicationBaseURI">local base URI</form:label></td>
<td><form:input id ="localbaseuri" class="text" path="localApplicationBaseURI" size="40" name="localbaseuri" disabled="true"/></td>
</c:otherwise>
</c:choose>
<td><form:errors path="localApplicationBaseURI" cssClass="error"/></td>
</tr>
</table>
<div class="form-row">
<input type="submit" value="submit"/>
</div>
</form:form>
</div>
和脚本
<script language="JavaScript">
function toggle()
{
if(document.myForm.compatibilityMode.checked == false)
{
document.myForm.localbaseuri.disabled = true;
document.myForm.localbaseurilabel.className = "labeldisabled";
}
else
{
document.myForm.localbaseuri.disabled = false;
document.myForm.localbaseurilabel.className = "labelenabled";
}
}
</script>
使用此代码,当复选框被选中时,字段被正确激活(反之亦然),但字段不会改变其样式。 我也尝试设置颜色属性,但没有结果。
document.myForm.localbaseurilabel.style.color = "grey";
我错过了什么?
最佳答案
document.getElementById('localbaseurilabel').style.color = "grey";
关于javascript - 使用javascript更改标签颜色的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8873147/