【问题标题】:Jquery Steps Wizard Select List ValidationJquery 步骤向导选择列表验证
【发布时间】:2014-01-15 10:40:18
【问题描述】:

我正在使用一个名为steps 的jQuery 插件来创建一个表单向导。 可以配合jQueryvalidation插件使用。

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

到目前为止,这是我的代码。 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' 作为一个类来验证,但遗憾的是这不适用于选择列表。

【问题讨论】:

    标签: javascript jquery validation jquery-validate jquery-steps


    【解决方案1】:

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

    $(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());
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      • 2023-03-14
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多