首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >隐藏字段的Coldfusion cfform验证

隐藏字段的Coldfusion cfform验证
EN

Stack Overflow用户
提问于 2013-04-05 19:06:22
回答 1查看 576关注 0票数 2

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

代码如下:

代码语言:javascript
复制
<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">

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-05 19:27:37

我认为最简单的解决方案可能是为opns1添加一个默认值,以满足在选择"other“之外的条件时所需的条件。

代码语言:javascript
复制
<cfinput type="text" name="opns1" id="opns1" value="other"  required="true"  message="Please enter your choice" >

在JS if (opval == "Other")条件中添加

代码语言:javascript
复制
document.getElementById('opns1').value=""; // to clear "other" from the input.

在表单处理程序中,检查以确保opns1 neq "other“

代码语言:javascript
复制
<cfif form.opns1 neq "other">
  do something with the new value
</cfif>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15832296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档