【问题标题】:Coldfusion cfform validation for hidden fields隐藏字段的 Coldfusion cfform 验证
【发布时间】:2013-04-05 11:06:22
【问题描述】:

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

代码如下:

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

【问题讨论】:

    标签: coldfusion cfform


    【解决方案1】:

    我认为最简单的解决方案可能是为 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 "other"

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多