首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优化Jquery代码

问优化Jquery代码
EN

Stack Overflow用户
提问于 2015-05-13 09:16:45
回答 1查看 54关注 0票数 1

我正在使用jquery智能向导脚本将我的表单变成向导。

这很好,但是验证代码看起来可以变得更简单。

这就是他们提供的例子。这是否可以变成一个循环,这样我就不必为我的5个步骤中的每一个步骤编写它了?

代码语言:javascript
复制
function validateSteps(step){
  var isStepValid = true;
   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});
    }
}

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

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-13 09:27:08

你可以这么做,

代码语言:javascript
复制
function validateSteps(){
        var isStepValid = true, step =1;
        for(step ;step <=5;step ++){
            if(window["validateStep"+step ]() == 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;

    }

您只需担心有一个名为validateStepX的函数

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30210486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档