2013-04-05 93 views
2

我有一个带有一些选项的选择框的表单。如果用户选择“其他”选项,它将显示用户必须输入其选择的隐藏文本字段。这两个表单字段都是必填字段。但是当我选择“其他”以外的其他选项时,验证失败。有人能帮我吗?隐藏字段的Coldfusion cfform验证

的代码如下:

<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"> 

回答

2

我认为最简单的解决方案可能是添加一个默认值opns1满足当选择除“其他”的东西所需要的条件。

<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 “其他”

<cfif form.opns1 neq "other"> 
    do something with the new value 
</cfif>