我有一个表单,它有一个带有一些选项的选择框。如果用户选择其他选项,它将显示隐藏的文本字段,用户必须在其中输入他的选择。这两个表单域都是必填域。但是当我选择除' other‘之外的任何其他选项时,验证失败。有人能帮帮我吗?
代码如下:
<script >
function opnSelect() {
var opval = document.test_form.opns.value;
if (opval == "Other") {
// document.getElementById('td_options').style.display='none';
document.getElementById('td_opns1').style.display="";
}
else if(opval!="Other"){
document.getElementById('td_opns1').style.display='none';
}
}
</script>
<cfform name="test_form">
<table>
<tr>
<td width="120" align="right"><font color="#FF0000"><b>Please select from options</b></font></td>
<td align="left" id="td_options">
<cfselect size="1" style="width:150px" name="opns" id="opns" required="true" message="Please select any value" onchange="javascript:opnSelect();">
<option value="" ></option>
<option value="value1">value1</option>
<option value="value2">value2</option>
<option value="value3">value3</option>
<option value="Other">Other</option>
</cfselect>
</td>
</tr>
<tr id="td_opns1" style="display:none;" >
<td align="right"><b><font color="#FF0000"> your choice</font></b></td>
<td align="left" >
<cfinput type="text" name="opns1" id="opns1" required="true" message="Please enter your choice" >
</td>
</tr>
</table>
<cfinput type="submit" value="submit" name="submit1">
发布于 2013-04-05 19:27:37
我认为最简单的解决方案可能是为opns1添加一个默认值,以满足在选择"other“之外的条件时所需的条件。
<cfinput type="text" name="opns1" id="opns1" value="other" required="true" message="Please enter your choice" >在JS if (opval == "Other")条件中添加
document.getElementById('opns1').value=""; // to clear "other" from the input.在表单处理程序中,检查以确保opns1 neq "other“
<cfif form.opns1 neq "other">
do something with the new value
</cfif>https://stackoverflow.com/questions/15832296
复制相似问题