逐步模式引导

时间:2018-09-18 13:01:27

标签: javascript jquery

我试图逐步验证每个步骤,但我只能验证输入错误,但不能验证选择。 希望当select的值等于零(0)时,也会进行验证。

有人可以帮我这个JS吗?我将非常感谢。

    $(document).ready(function () {
    
        var navListItems = $('div.setup-panel div a'),
                allWells = $('.setup-content'),
                allNextBtn = $('.nextBtn');
    
        allWells.hide();
    
        navListItems.click(function (e) {
            e.preventDefault();
            var $target = $($(this).attr('href')),
                    $item = $(this);
    
            if (!$item.hasClass('disabled')) {
                navListItems.removeClass('btn-primary').addClass('btn-default');
                $item.addClass('btn-primary');
                allWells.hide();
                $target.show();
                $target.find('input:eq(0)').focus();
                $target.find('select:eq(0)').focus();
            }
        });
    
        allNextBtn.click(function(){
            var curStep = $(this).closest(".setup-content"),
                curStepBtn = curStep.attr("id"),
                nextStepWizard = $('div.setup-panel div a[href="#' + curStepBtn + '"]').parent().next().children("a"),
                curInputs = curStep.find("input[type='text'],input[type='url'],select[type='text']"),
                isValid = true;
    
            $(".form-group").removeClass("has-error");
            for(var i=0; i<curInputs.length; i++){
                if (!curInputs[i].validity.valid){
                    isValid = false;
                    $(curInputs[i]).closest(".form-group").addClass("has-error");
                }
            }
    
            if (isValid)
                nextStepWizard.removeAttr('disabled').trigger('click');
        });
    
        $('div.setup-panel div a.btn-primary').trigger('click');
    });
<div class="container">
        <div class="row">
          <h2>Multistep form wizard on modal</h2>
          <div class="center"><button data-toggle="modal" data-target="#requestform" class="btn btn-primary center-block">Click Me</button></div>
        </div>
      </div>
    
      <div class="modal fade" id="requestform" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
              <h3 class="modal-title" id="lineModalLabel">Proposta</h3>
            </div>
            <div class="modal-body">
              <!-- Steps starts here -->
              <div class="requestwizard">
                <div class="requestwizard-row setup-panel">
                  <div class="requestwizard-step">
                    <a href="#step-1" type="button" class="btn btn-primary btn-circle">1</a>
                    <p>Selecione um contato</p>
                  </div>
                  <div class="requestwizard-step">
                    <a href="#step-2" type="button" class="btn btn-default btn-circle" disabled="disabled">2</a>
                    <p>Selecione um curso</p>
                  </div>
                  <div class="requestwizard-step">
                    <a href="#step-3" type="button" class="btn btn-default btn-circle" disabled="disabled">3</a>
                    <p>Forma de pagamento</p>
                  </div>
                </div>
              </div>
    
              <br>
              <form role="form">
                <div class="row setup-content" id="step-1">
                  <div class="col-xs-12">
                    <div class="col-md-12">
                      <div class="form-group">
                        <input  maxlength="100" type="text" required="required" class="form-control" placeholder="Contato"  />
                      </div>
                      <hr>
                      <button class="btn btn-primary nextBtn btn-lg pull-right" type="button" >Próximo</button>
                    </div>
                  </div>
                </div>
                <div class="row setup-content" id="step-2">
                  <div class="col-xs-12">
                    <div class="col-md-12">
                      <div class="row">
                        <div class="col-md-4">
                          <div class="form-group">
                            <select class="form-control custom-select" id="filtercountry">
                              <option value="0" class="selectcountry">Country</option>
                              <option value="201" class="selectcountry">Australia</option>
                            </select>
                          </div>
                        </div>
                        <div class="col-md-4">
                          <div class="form-group">
                            <select class="form-control custom-select" id="filtercity">
                              <option value="0"  class="selectcity">City</option>
                              <option value="101"  class="selectcity">California</option>
                            </select>
                          </div>
                        </div>
                        <div class="col-md-4">
                          <div class="form-group">
                            <select class="form-control custom-select" id="filterregion">
                              <option value="0"   class="selectregion">Region</option>
                              <option value="3" class="selectregion">Brazil</option>
                            </select>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <input maxlength="200" type="text" required="required" class="form-control" placeholder="Curso"  />
                      </div>
                      <button class="btn btn-primary nextBtn btn-lg pull-right" type="button" >Próximo</button>
                    </div>
                  </div>
                </div>
                <div class="row setup-content" id="step-3">
                  <div class="col-xs-12">
                    <div class="col-md-12">
                      <div class="row">
                        <div class="col-md-4">
                          <div class="form-group">
                            <label class="control-label"><spring:message code="label.firstinstallment"/></label>
                            <input type="text" class="form-control" placeholder="First installment" id="finstallment" name="finstallment" required>
                          </div>
                        </div>
                        <div class="col-md-4">
                          <div class="form-group">
                            <label class="control-label"><spring:message code="label.numberparcels"/></label>
                            <select class="form-control custom-select" name="parcels" required>
                              <option value="0">0</option>
                              <option value="1">1</option>
                            </select>
                          </div>
                        </div>
                        <div class="col-md-4">
                          <div class="form-group">
                            <label class="control-label"><spring:message code="label.firstinvoice"/></label>
                            <input type="date" class="form-control" placeholder="Invoice maturity" name="expiration_date" required>
                          </div>
                        </div>
                        <div class="col-md-12">
                          <div class="form-group">
                            <label class="control-label"><spring:message code="label.selectpaymentplan"/></label>
                            <select class="form-control custom-select" name="payment_plan" required>
                              <option value="1">Bank slip</option>
                            </select>
                          </div>
                        </div>
                      </div>
                      <button class="btn btn-primary nextBtn btn-lg pull-right" type="button" >Enviar</button>
                    </div>
                  </div>
                </div>
              </form>
    
    
              <!-- Form ends here -->
            </div>
          </div>
        </div>
      </div>

1 个答案:

答案 0 :(得分:0)


您需要在 allNextBtn.click 事件中的for循环中检查控件类型,如下所示:

    for(var i=0; i<curInputs.length; i++){

        //if the control is select check the selected index if it is ZERO
        if ( $(curInputs[i]).is('select') && (curInputs[i].selectedIndex === 0)
        {
            isValid = false;
        }
        //else check control validity
        else if (!curInputs[i].validity.valid)
        {
            isValid = false;
        }

        if(!isValid)
        {
            $(curInputs[i]).closest(".form-group").addClass("has-error");
        }
    }

并更改此行:

curInputs = curStep.find("input[type='text'],input[type='url'],select[type='text']"),

对此:

curInputs = curStep.find("input[type='text'],input[type='url'],select"),

这是完整的js代码:

$(document).ready(function () {

    var navListItems = $('div.setup-panel div a'),
            allWells = $('.setup-content'),
            allNextBtn = $('.nextBtn');

    allWells.hide();

    navListItems.click(function (e) {
        e.preventDefault();
        var $target = $($(this).attr('href')),
                $item = $(this);

        if (!$item.hasClass('disabled')) {
            navListItems.removeClass('btn-primary').addClass('btn-default');
            $item.addClass('btn-primary');
            allWells.hide();
            $target.show();
            $target.find('input:eq(0)').focus();
            $target.find('select:eq(0)').focus();
        }
    });

    allNextBtn.click(function(){
        var curStep = $(this).closest(".setup-content"),
            curStepBtn = curStep.attr("id"),
            nextStepWizard = $('div.setup-panel div a[href="#' + curStepBtn + '"]').parent().next().children("a"),
            curInputs = curStep.find("input[type='text'],input[type='url'],select"),
            isValid = true;

        $(".form-group").removeClass("has-error");

        //BEGIN EDIT
        for(var i=0; i<curInputs.length; i++){

            //if the control is select check the selected index if it is ZERO
            if ( $(curInputs[i]).is('select') && (curInputs[i].selectedIndex === 0))
            {
                isValid = false;
            }
            //else check control validity
            else if (!curInputs[i].validity.valid)
            {
                isValid = false;
            }

            if(!isValid)
            {
                $(curInputs[i]).closest(".form-group").addClass("has-error");
            }
        }
        //END EDIT

        if (isValid)
            nextStepWizard.removeAttr('disabled').trigger('click');
    });

    $('div.setup-panel div a.btn-primary').trigger('click');
});