我正在使用jquery智能向导脚本将我的表单变成向导。
这很好,但是验证代码看起来可以变得更简单。
这就是他们提供的例子。这是否可以变成一个循环,这样我就不必为我的5个步骤中的每一个步骤编写它了?
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;
}谢谢
发布于 2015-05-13 09:27:08
你可以这么做,
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的函数
https://stackoverflow.com/questions/30210486
复制相似问题