0

我正在使用 Jquery smartwizard链接,当用户在任何步骤中单击“上一步”按钮时,我需要停止验证(默认情况下,第一步中的上一步按钮被禁用)除了第一步。

这是我的javascript代码

$(document).ready(function () {
      // Smart Wizard       
      $('#wizard').smartWizard({
          transitionEffect: 'fade',
          onLeaveStep: leaveAStepCallback,
          onFinish: onFinishCallback,
          enableFinishButton: false
      });

      function leaveAStepCallback(obj) {
          var step_num = obj.attr('rel');
          return validateSteps(step_num);
      }

      function onFinishCallback() {
          if (validateAllSteps()) {
              $('form').submit();
          }
      }
  });

  function validateAllSteps() {
      var isStepValid = true;
      if (validateStep1() == false) {
          isStepValid = false;
          $('#wizard').smartWizard('setError', {
              stepnum: 1,
              iserror: true
          });
      } else {
          $('#wizard').smartWizard('setError', {
              stepnum: 1,
              iserror: false
          });
      }
      if (validateStep2() == false) {
          isStepValid = false;
          $('#wizard').smartWizard('setError', {
              stepnum: 2,
              iserror: true
          });
      } else {
          $('#wizard').smartWizard('setError', {
              stepnum: 2,
              iserror: false
          });
      }
      if (validateStep3() == false) {
          isStepValid = false;
          $('#wizard').smartWizard('setError', {
              stepnum: 3,
              iserror: true
          });
      } else {
          $('#wizard').smartWizard('setError', {
              stepnum: 3,
              iserror: false
          });
      }
      if (!isStepValid) {
          $('#wizard').smartWizard('showMessage', 'Please correct the errors in the steps and continue');
      }
      return isStepValid;
  }

  function validateSteps(step) {
      var isStepValid = true;
      // validate step 1
      if (step == 1) {
          if (validateStep1() == false) {
              isStepValid = false;
              $('#wizard').smartWizard('showMessage', 'Please correct the errors in step' + step + ' and click next.');
              $('#wizard').smartWizard('setError', {
                  stepnum: step,
                  iserror: true
              });
          } else {
              $('#wizard').smartWizard('hideMessage');
              $('#wizard').smartWizard('setError', {
                  stepnum: step,
                  iserror: false
              });
          }
      }
      // validate step2
      if (step == 2) {
          if (validateStep2() == false) {
              isStepValid = false;
              $('#wizard').smartWizard('showMessage', 'Please correct the errors in step' + step + ' and click next.');
              $('#wizard').smartWizard('setError', {
                  stepnum: step,
                  iserror: true
              });
          } else {
              $('#wizard').smartWizard('hideMessage');
              $('#wizard').smartWizard('setError', {
                  stepnum: step,
                  iserror: false
              });
          }
      }
      // validate step3
      if (step == 3) {
          if (validateStep3() == false) {
              isStepValid = false;
              $('#wizard').smartWizard('showMessage', 'Please correct the errors in step' + step + ' and click next.');
              $('#wizard').smartWizard('setError', {
                  stepnum: step,
                  iserror: true
              });
          } else {
              $('#wizard').smartWizard('hideMessage');
              $('#wizard').smartWizard('setError', {
                  stepnum: step,
                  iserror: false
              });
          }
      }
      return isStepValid;
  }

  function validateStep1() {
      //Validation code here
  }

  function validateStep2() {
      //Validation code here
  }

  function validateStep3() {
      //Validation code here
  }
4

2 回答 2

0

...一个非常合理的要求。尝试这个...

  function leaveAStepCallback(anchor, context) {
      //var step_num = obj.attr('rel');
      if(context.toStep > context.fromStep)
          return validateSteps(fromStep);
      else
          return true;
  }

文档建议您可以从第二个参数中获取 fromStep 和 toStep,如图所示。我希望是这样!如果没有,请四处寻找。它一定在某个地方。

于 2015-11-17T09:34:10.197 回答
0

您可以同时检查 fromStep 和 toStep:

function nextStep(smartWizard, steps, context){
                if(steps.fromStep == 1 && steps.toStep == 2){
                    // Do validations
                }
}

我使用了以下向导:

$('#wizard').smartWizard({
                    onLeaveStep: nextStep // triggers when leaving a step
            });
于 2017-11-06T10:26:46.923 回答