2015-11-17 66 views
0

我正在使用Jquery smartwizard link,我需要停止驗證,當用戶在任何步驟中單擊「上一步」按鈕(默認情況下在第1步中上一個按鈕被禁用)除第1步。如何跳過驗證時向後 - 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 
    } 
+0

檢查此鏈接http://stackoverflow.com/questions/9925255/skip-validation -when-back-in-smartwizard – Suyog

回答

0

...一個很合理的要求。試試這個...

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

docs建議你可以從第二個參數得到fromStep和toStep如圖所示。我希望情況就是這樣!如果不是,那就去尋找它。它一定在那裏。

+0

當我使用這個表單將不經驗證地提交 – user3635258

+0

不。你有onFinish事件來驗證提交之前的一切。 – bbsimonbb

+0

這項工作? – bbsimonbb

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