0

这是我的 jQuery 片段

  $("#saveBankDetails").click(function(){
           if($('#firstBankDetail').is(':visible')) {
               return validateFirstBankDetails();
            }     
           if($('#secondBankDetail').is(':visible')) {
               return validateSecondBankDetails();
            }   
           if($('#thirdBankDetail').is(':visible')) {
               return validateThirdBankDetails();
            }   
           if($('#fourthBankDetail').is(':visible')) {
               return validateFourthBankDetails();
            }   
       });

如果元素 firstBankDetail , secondBankDetail 等可见,我想要实现的是执行验证。validateFirstBankDetailsvalidateSecondBankDetails都是javascript函数。

如果验证失败,我使用 return 来避免将数据发布到服务器。但是,如果 validateFirstBankDetails() 返回 true,似乎会发布数据,因此代码不会继续前进。我需要做哪些修改才能使代码继续前进并为 secondBankDetail 应用验证?

4

7 回答 7

1

将结果收集到单个变量:

$("#saveBankDetails").click(function(){
           var restult = true;
           if($('#firstBankDetail').is(':visible')) {
               result &= validateFirstBankDetails();
            }     
           if($('#secondBankDetail').is(':visible')) {
               result &= validateSecondBankDetails();
            }   
           if($('#thirdBankDetail').is(':visible')) {
               result &= validateThirdBankDetails();
            }   
           if($('#fourthBankDetail').is(':visible')) {
               result &= validateFourthBankDetails();
            }   

           return result;
       });

在这种情况下,如果至少一个验证失败 - 整个表单验证将失败。

PS最好订阅form提交事件并在那里处理验证: Prevent Default on Form Submit jQuery

于 2013-10-04T10:46:31.317 回答
0

你也可以试试这个

$("#saveBankDetails").click(function(){
       var result = false;
       if($('#firstBankDetail').is(':visible')) {
           result = validateFirstBankDetails();
        }     
       if($('#secondBankDetail').is(':visible')) {
           result = validateSecondBankDetails();
        }   
       if($('#thirdBankDetail').is(':visible')) {
           result = validateThirdBankDetails();
        }   
       if($('#fourthBankDetail').is(':visible')) {
           result = validateFourthBankDetails();
        }
       return result;
   });
于 2013-10-04T10:48:45.830 回答
0

给这些元素 aclass然后循环它们:

$("#saveBankDetails").click(function(){
    var all_elements = $('.newClass');
    all_elements.each(function () {
        if (!$(this).is(':visible')) {
            //do something if not visible
            return false;
        }
    });
});
于 2013-10-04T10:49:11.140 回答
0

After following @Samich advice i came up with this solution. It works fine.

$('form').submit(function (event) {
           if($('#firstBankDetail').is(':visible')) {
               if(!validateFirstBankDetails()){
                   event.preventDefault();
               }
            }  
           if($('#secondBankDetail').is(':visible')) {
              if(!validateSecondBankDetails()){
               event.preventDefault();
              }
            }   
           if($('#thirdBankDetail').is(':visible')) {
               if(!validateThirdBankDetails()){
                   event.preventDefault();
               }
            }   
           if($('#fourthBankDetail').is(':visible')) {
               if(!validateFourthBankDetails()){
                   event.preventDefault();
               }
            }
       });

It is easier too

于 2013-10-04T11:31:18.117 回答
0

如果验证失败

return false 

将此添加到单击事件的末尾。

于 2013-10-04T10:46:30.300 回答
-1
$("#saveBankDetails").click(function(){
       var result = 0; var visible = 0;
       if($('#firstBankDetail').is(':visible')) {
           result = result + ( 1 & validateFirstBankDetails());
           visible++;
        }     
       if($('#secondBankDetail').is(':visible')) {
           result = result + ( 1 & validateFirstBankDetails());
           visible++;
        }   
       if($('#thirdBankDetail').is(':visible')) {
           result = result + ( 1 & validateFirstBankDetails());
           visible++;
        }   
       if($('#fourthBankDetail').is(':visible')) {
           result = result + ( 1 & validateFirstBankDetails());
           visible++;
        }   
       if(result == visible) {return true;}
       else{return false;}
   });
于 2013-10-04T10:58:58.290 回答
-1
 $("#saveBankDetails").click(function(){
       if($('#firstBankDetail').is(':visible')) {
           return validateFirstBankDetails();
        }     
       if($('#secondBankDetail').is(':visible')) {
           return validateSecondBankDetails();
        }   
       if($('#thirdBankDetail').is(':visible')) {
           return validateThirdBankDetails();
        }   
       if($('#fourthBankDetail').is(':visible')) {
           return validateFourthBankDetails();
        } 
       return false;

   });

如果您不想将控制权返回给服务器,您可以添加 return false;

于 2013-10-04T10:47:49.560 回答