我正在使用Struts,这是JSP文件中的复选框
<th class="input required">Check Box</th>
<td class="input" colspan="4">
<html:checkbox property="chkb1" value="1" styleId="chkb_opt01"></html:checkbox>
<label for="chkb_opt01" class="width80">checkbox1</label>
<html:checkbox property="chkb2" value="5" styleId="chkb_opt05"></html:checkbox>
<label for="chkb_opt05" class="width80">checkbox2</label>
</td>
我已经尝试编写JavaScript执行复选框验证。至少选中了一个复选框。
答案 0 :(得分:1)
如果您在任何浏览器中检查html的来源。或者,如果你可以在firefox中使用firebug,在opera中使用dragonfly,使用IE和Chrome开发工具并检查元素。自定义标签无论是struts标签还是其他标签,都会生成与普通HTML相同的HTML。
e.g。
<html:checkbox property="chkb1" value="1" styleId="chkb_opt01"></html:checkbox>
将转换为
<input type="checkbox" name="chkb1" id="chkb_opt01"></input>
要使用javascript进行验证,您可以使用name或id属性。
e.g。
使用名称属性
if(document.getElementsByName("chkb1")[0].checked)
{
alert("This is checked.. ");
}
else
{
alert("Oops!!! You are not checked")
}
使用id属性
if(document.getElementById("chkb_opt01").checked)
{
alert("This is checked.. ");
}
else
{
alert("Oops!!! You are not checked")
}