2

我正在使用一个名为steps的 jQuery 插件来创建表单向导。可以与 jQuery验证插件一起使用。

在用户继续下一组选项之前,我需要能够验证选择列表,但我还没有找到这样做的方法。

到目前为止,这是我的代码。的HTML

<h2>Engagement</h2>
   <div id = "pane1">
      <div class = "panel panel-default">
        <div class = "panel-heading">
          Engagement
           </div>
             <div class = "panel-body">
               <table class='table table-hover'>
                <tr valign='top'><td class='tableLabel'>Request Type:</td><td><select onchange = "getType();"  id = "type" class="selectpicker required" name='type'>
 <?php
foreach($types AS $key => $val){
    echo "<option value='{$key}'>{$val}</option>"; 
   }
?>
</select></td></tr>  
<tr valign='top'><td class='tableLabel'>Delivery Date:</td><td><input  id='deliveryDate' placeholder="Select a delivery date for your request"  class="form-control datePicker required" type='text' name='devlireyDate'></td></tr>
</table> 
 </div> 
</div>
</div>


            <h2>Details</h2>

            <div id = "pane2">

            </div>
         </form>

jQuery

$(function (){
     $("#engagementForm").steps({
     headerTag: "h2",
     bodyTag: "div",
     transitionEffect: "slideLeft",
     onStepChanging: function (event, currentIndex, newIndex)
    {
    // Always allow going backward even if the current step contains invalid fields!
    if (currentIndex > newIndex)
    {
        return true;
    }



    var form = $(this);

    // Clean up if user went backward before
    if (currentIndex < newIndex)
    {
        // To remove error styles
        $(".body:eq(" + newIndex + ") label.error", form).remove();
        $(".body:eq(" + newIndex + ") .error", form).removeClass("error");
    }

    // Disable validation on fields that are disabled or hidden.
    form.validate().settings.ignore = ":disabled,:hidden";

    // Start validation; Prevent going forward if false
                return form.valid();
            },
            onFinishing: function (event, currentIndex)
            {
                var form = $(this);

                // Disable validation on fields that are disabled.
                // At this point it's recommended to do an overall check (mean ignoring only disabled fields)
                form.validate().settings.ignore = ":disabled";

                // Start validation; Prevent form submission if false
                return form.valid();
            },
            onFinished: function (event, currentIndex)
            {
                var form = $(this);

                // Submit form input
                form.submit();
            }
        });

                 $('.selectpicker').selectpicker();
                 $(".datePicker").datepicker({ dateFormat: 'yy-mm-dd' , minDate: "+1d" });
            });

交货日期只需通过 puttint 'required' 作为一个类来验证,但遗憾的是,这不适用于选择列表。

4

1 回答 1

0

一种解决方法是将更改时选择的值保存到隐藏输入并验证隐藏输入。未经测试,但应该可以工作。

$(document).ready(function(){
    $('#hidden_input').val('');
    form.validate({
        ignore: [], //make hidden works with jQuery validation and not ignore any hidden field
        rules: {
           hidden_input: required
        },
        messages: { ... }
        showErrors: function (errorMap, errorList) {
            $.each(this.successList, function (index, value) {

                 if (value.id == "hidden_input"){
                     $('#select').tooltip('destroy');               
                 }else{
                     $('#'+value.id+'').tooltip('destroy');
                 }
            }); 

            $.each(errorList, function (index, value) {
                 if (value.id == "hidden_input"){
                     $('#select').attr('title',value.message).tooltip({
                        placement: 'left',
                        trigger: 'manual'
                     }).tooltip('show');                
                 }else{
                     $('#'+value.element.id).attr('title',value.message).tooltip({
                        placement: 'left',
                        trigger: 'manual'
                     }).tooltip('show');                
                 }
        }
    });

    //define your jQuery steps here
    form.steps({ ... });

    //save value to hidden field on change of select 
    $('#select').on('change', function(){
         $("#hidden_input").val($("#select").val());
    });
});
于 2015-10-30T17:13:12.907 回答