bootstrap multiselect插件未在多页上正确验证

时间:2016-04-08 07:38:17

标签: javascript jquery twitter-bootstrap jquery-validate bootstrap-multiselect

我正在创建一个多页表单并使用davidstutz bootstrap multiselect插件来选择选项。我在第一页上使用2多选,在第3页上使用2。最初它验证很好,但是如果我在到达第三页后返回上一页,则下一个按钮停止工作,因为它也验证了第三页上的多选项。

这是用于多选的代码:

 $('#divisionList').multiselect({
    maxHeight: 150,
    //enableCaseInsensitiveFiltering: true,
    numberDisplayed: 1,
    nonSelectedText: '--Select Division--',
    disableIfEmpty: true,
    buttonClass: 'btn btn-default',
    buttonContainer: '<div class="btn-group btn-group-justified" />',
    //disabledText: 'There is no WorkItem SubHead...'
    onChange: function (element, checked) {
        $(element).closest('.multiselect').valid();
        if (checked === true) {
            $(document).click();
        } else if (checked === false) {

        }
    },
    templates: {
        button: '<a type="button" class="multiselect dropdown-toggle multiselect-title-text" style="width:95%;" data-toggle="dropdown"><span class="multiselect-selected-text"></span></a><a type="button" class="multiselect dropdown-toggle" style="width:5%;" data-toggle="dropdown"><span class="caret"></span></a>'
    }
});

验证:

  $('#addDbaProjectForm').validate({ // initialize plugin
    ignore: ':hidden:not(".multiselect")',   //.multiselect is the class of select tag
    rules: {
        districtList: "required",
        divisionList: "required",
        projectName: {
            required: true,
            noSpace: true
        },
        subProjectName: {
            required: true,
            noSpace: true
        },
        chainageOfProject: {
            required: true,
            noSpace: true
        }
   }
  });

下一个按钮工作如下:

     //Step 1
        if (current == 1) {
            // Check validation
            if ($('#addDbaProjectForm').valid()) {
                widget.show();
                widget.not(':eq(' + (current++) + ')').hide();
                setProgress(current);
            }
        }

1 个答案:

答案 0 :(得分:0)

WOrking Fiddle - https://jsfiddle.net/bdq4a86o/13/

1)对你的小提琴所做的改变:

1)从rawgit.com为bootstrap-multiselect.js和bootstrap-multiselect.css文件添加了生产URL。

2)用IIF

替换If

2)您需要在代码中进行的更改:

1)根据步骤对表单中的控件进行分组,添加class&#34; step1&#34;对于step1控件和&#34; step2&#34; step2控件的类

<select class="form-control multiselect step1" name="districtList" id="districtList">
                          <option value="">--Select--</option>
                          <option value="A">A</option>
                          <option value="B">B</option>
                          <option value="C">C</option>
                          <option value="D">D</option>
                        </select>

2)当&#34; next&#34;时,您需要仅验证该部分。单击按钮。所以你需要在每个类上调用.valid()方法。例如: - if ($('.step1').valid())

//Step 1
  if (current == 1) {
    // Check validation
    if ($('.step1').valid()) {
      widget.show();
      widget.not(':eq(' + (current++) + ')').hide();
      setProgress(current);
    }
  }

  //Step 2
  else if (current == 2) {
    if($('.step2').valid()) {
      widget.show();
      widget.not(':eq(' + (current++) + ')').hide();
      setProgress(current);
    }
  }

  //Step 3
  else if (current == 3) {
    if ($('.step3').valid()) {
      widget.show();
      widget.not(':eq(' + (current++) + ')').hide();
      setProgress(current);
    }
  }

3)需要相应添加跳过按钮的代码。