【问题标题】:required field validator for hidden dropdown selection隐藏下拉选择的必填字段验证器
【发布时间】:2014-04-22 16:15:56
【问题描述】:

我有下面的代码,所以当访问者选择“国内”时,国内会出现一个隐藏的下拉选择。如果访问者选择“学生”,则会出现另一个隐藏的下拉选项。要查看它是如何工作的,您可以check it out here 我还使用了一个 jquery 验证器,所以如果没有选择一个字段或下拉菜单,那么它会弹出一个窗口。但是因为只能选择一个隐藏字段(与学生或家庭相关的字段),所以这个验证器总是会弹出。如果没有选择这些隐藏的下拉菜单的选项,我该怎么做才能使弹出窗口起作用。

我使用的 jquery 验证器如下:

<script>


function formCheck(formobj){

var x=document.forms["form1"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");

var fieldRequired = Array("reason1",  "reason2");
// Enter field description to appear in the dialog box



var fieldDescription = Array("Reason 1",  "Reason 2");

// dialog message
var alertMsg = "Please complete all fields and enter a valid email address";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
    var obj = formobj.elements[fieldRequired[i]];
    if (obj){
        switch(obj.type){
        case "select-one":
            if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].value == ""){
                alertMsg += "  " + "\n";
            }
            break;
        case "select-multiple":
            if (obj.selectedIndex == -1){
                alertMsg += "  " + "\n";
            }
            break;

        case "text":
            case "textarea":
            if (obj.value == "" || obj.value == null || atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){
                alertMsg += "  " +  "\n";
            }
            break;

        default:
        }
        if (obj.type == undefined){
            var blnchecked = false;
            for (var j = 0; j < obj.length; j++){
                if (obj[j].checked){
                    blnchecked = true;
                }
            }
            if (!blnchecked){
                alertMsg += "  "  + "\n";
            }
        }
    }
}

if (alertMsg.length == l_Msg){
    return true;
}else{
    alert(alertMsg);
    return false;
}


}
// -->
</script>

html部分的代码如下:

<script type="text/javascript">

    /* <![CDATA[ */
    $(document).ready(function(){

        $("#storagetype").change(function(){

            if ($(this).val() == "student" ) {

                $("#hidestudent").slideDown("fast"); //Slide Down Effect

            } else {

                $("#hidestudent").slideUp("fast");  //Slide Up Effect

            }
        });


        $("#storagetype").change(function(){

            if ($(this).val() == "domestic" ) {

                $("#hidedomestic").slideDown("fast"); //Slide Down Effect

            } else {

                $("#hidedomestic").slideUp("fast"); //Slide Up Effect

            }
        });
    });

    /* ]]> */

    </script>


    <div class="contactform">
    <div class="forminside">
    <div id="apDiv1">

    <form id="form1" name="form1" method="get" form action="page2.php"  onsubmit="return formCheck(this); "> 

    <div class="detailscolumn1"><h3></h3>
<br/>
<br/>

   <div class="storagetype">
            <div class="input select">
             <div class="labelfortype">
      <label for="storagetype">    select your business type</label>    </div><!--end of labelfortype class-->
                    <select name="storagetype" id="storagetype">
                <option value="">(select)</option>
                <option value="domestic">domestic</option>
                <option value="student">student</option>
                </select>
        </div>
       </div><!--end of storagetype class-->

     <div class="hiddenreason">   
        <div class="hide" id="hidedomestic"><!-- this select box will be hidden at first -->

            <div class="input select">
 <div class="labelforreason"> <label for="reasonstorage">reason for business</label></div><!--end of labelfortype class-->

                <select name="reason2" id="reason1">
                    <option value="">(select)</option>
                    <option value="moving">moving</option>
                    <option value="diy/home improvements">diy/home improvements</option>
                    <option value="decluttering">decluttering</option>
                    <option value="other">other</option>
                </select>
            </div>
        </div>
       </div><!--end of hidden reason--> 

      <div class="hiddenreason">  
       <div class="hide" id="hidestudent"><!-- this select box will be hidden at first -->
            <div class="input select">
  <div class="labelforreason"> <label for="reasonstorage">reason for business</label></div><!--end of labelfortype class-->

                <select name="reason3" id="reason2">
                    <option value="">(select)</option>
                    <option value="holiday">holiday</option>
                    <option value="moving">moving</option>
                    <option value="other">other</option>

                </select>
            </div>
        </div>
     </div><!--end of hidden reason-->    






    </div><!--end of detailscolumn1-->



    </form></div>

    </div><!--end of forminside-->
    </div><!--end of contactform-->

在此先感谢

【问题讨论】:

  • 尝试添加0作为值如果没有被选中,它更容易检查它,然后确保你禁用隐藏的select元素,最后只检查没有的值禁用...
  • @webeno 感谢您的评论。值 0 已经存在。我遇到的问题与上面的验证器有关。它期望选择隐藏的下拉选择。但只能选择其中一个,家庭或学生相关的。
  • 你挂断了0 那里...看看我写的第二部分... ;)
  • 感谢@webeno 这很有意义。以及如何在隐藏时禁用选择并仅在未禁用时才检查值?我仍在学习这个主题,因此非常感谢您的帮助。
  • 我提供了一个答案......我之前不想这样做的原因是我没有在你的 js 函数中看到你在哪里对实际进行验证选择的值......我仍然不知道,所以我添加了一个通用的回应......希望它会有所帮助......

标签: jquery forms drop-down-menu hidden-field requiredfieldvalidator


【解决方案1】:

我建议你禁用像这样隐藏的选择:

$("#storagetype").change(function(){
    if ($(this).val() == "student" ) {
        $("#hidestudent").slideDown("fast"); //Slide Down Effect
        $("#reason2").prop('disabled', false);
    } else {
        $("#hidestudent").slideUp("fast");  //Slide Up Effect
        $("#reason2").prop('disabled', true);
    }
});

(同样适用于另一个)

...然后在您进行验证时检查输入的禁用状态(老实说,我无法从您的 js 代码中找出您正在执行此操作的位置,所以我在这里给出一个一般示例):

if ($('#test').prop('disabled') == true) {
    // do stuff if true
} else {
    // do stuff if false
}

已编辑:您的 js 代码一定有内在错误(抱歉,我无法弄清楚究竟是什么)但是当我测试它时,它总是返回错误消息,即使我删除隐藏隐藏 div 的行并允许它们显示,然后为所有选择值。

也没有电子邮件字段(在您的 js 代码中指定和检查 - 我添加了它),也没有提交按钮(也添加了)。

在这里查看我的测试:http://jsfiddle.net/Pk6SP/

【讨论】:

    猜你喜欢
    • 2015-12-29
    • 2014-04-14
    • 2012-06-29
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    相关资源
    最近更新 更多