条件选择使用jQuery和Validation插件

时间:2011-01-03 23:26:14

标签: jquery forms jquery-validate

我有一个表单,我正在使用jQuery验证插件进行验证。我想添加一个条件选择框(根据另一个选择填充/显示的选择框)并让它也进行验证。以下是我到目前为止的情况:

$(document).ready(function(){
      $("#customer_information").validate({
      //disable the submit button after it is clicked to prevent multiple submissions
          submitHandler: function(form){
          if(!this.wasSent){
              this.wasSent = true;
              $(':submit', form).val('Please wait...')
                    .attr('disabled', 'disabled')
                    .addClass('disabled');
              form.submit();
          } else {
              return false;
          }
          },
          //Customizes error placement
          errorPlacement: function(error, element) {
          error.insertAfter(element)
          error.wrap("<div class=\"form_error\">")
          }

      });

      $(".courses").hide();
      $("#course_select").change(function() {
      switch($(this).val()){
          case "Certificates":
          $(".courses").hide().parent().find("#Certificates").show();
          $(".filler").hide();
          break;
          case "Associates":
          $(".courses").hide().parent().find("#Associates").show();
          $(".filler").hide();
          break;
          case "":
          $(".filler").show();
          $(".courses").hide();
      }
      });

});

HTML:

  <select id="course_select">
    <option value="">Please Select</option>
    <option value="Certificates">Certificates</option>
    <option value="Associates">Associates</option>
  </select>

  <div id="Form0" class="filler"><select name="filler_select"><option value="">Please Select Course Type</option></select></div>
  <div id="Associates" class="courses">
    <select name="lead_source_id" id="Requested Program" class="required">
      <option value="">Please Select</option>
      <option value="01">Health Information Technology</option>
      <option value="02">Human Resources </option>
      <option value="03">Marketing </option>
    </select>
  </div>
  <div id="Certificates" class="courses">
      <select name="lead_source_id" id="Requested Program" class="required">
      <option value="">Please Select</option>
      <option value="04">Accounting Services</option>
      <option value="05">Bookkeeping</option>
      <option value="06">Child Day Care</option>
    </select>
  </div>

到目前为止,select正在为我工​​作,但验证认为即使选择了值,该字段也是空的。

看起来有很多方法可以在jQuery中进行条件选择。这是我设法解决的最好方式(我是jQuery的新手),但我很想听听你们认为“最好”的方式,特别是如果它与验证插件配合得很好。谢谢!

1 个答案:

答案 0 :(得分:1)

需要进行两项更改:

1)两个子选择框都分配了相同的名称属性,分配不同的名称   每个选择框的属性(这将用于分配验证规则):

   <div id="Associates" class="courses">
  <select name="lead_source_id_1" id="Requested Program" class="required">
    <option value="">Please Select</option>
    <option value="01">Health Information Technology</option>
    <option value="02">Human Resources </option>
    <option value="03">Marketing </option>
  </select>
</div>
<div id="Certificates" class="courses">
  <select name="lead_source_id_2" id="Select1" class="required">
    <option value="">Please Select</option>
    <option value="04">Accounting Services</option>
    <option value="05">Bookkeeping</option>
    <option value="06">Child Day Care</option>
  </select>
</div>
<input type="hidden" name="lead_source_id" id="lead_source_id" value="" />

2)为validate方法选项中的每个子选择框添加所需的依赖关系规则,如下所示:

    $(document).ready(function(){
        $("#customer_information").validate({
                rules:{
                    lead_source_id_1: {
                        required: function(element){
                            var retVal = ($("#course_select").val() == "Associates");
                            return retVal;
                        }
                    },
                    lead_source_id_2: {
                        required: function(element){
                            var retVal = ($("#course_select").val() == "Certificates");
                            return retVal;
                        }
                    }
                },
        //disable the submit button after it is clicked to prevent multiple submissions
                submitHandler: function(form){
                if(!this.wasSent){
                        this.wasSent = true;
                        $(':submit', form).val('Please wait...')
                                    .attr('disabled', 'disabled')
                                    .addClass('disabled');
                        form.submit();
                        //return false;
                } else {
                        return false;
                }
                },
                //Customizes error placement
                errorPlacement: function(error, element) {
                error.insertAfter(element)
                error.wrap("<div class=\"form_error\">")
                }

        });


$(".courses").hide();
$("#course_select").change(function () {
    switch ($(this).val()) {
    case "Certificates":
        $(".courses").hide().parent().find("#Certificates").show();
        $(".filler").hide();
        break;
    case "Associates":
        $(".courses").hide().parent().find("#Associates").show();
        $(".filler").hide();
        break;
    case "":
        $(".filler").show();
        $(".courses").hide();
    }
});

$(".courses select").change(function () {
                $("#lead_source_id").val($(this).val());
    });
});