0

这是我的 jQuery 片段

 $("#firstBankDetail").hide();
       $("#secondBankDetail").hide();
       $("#thirdBankDetail").hide();
       $("#fourthBankDetail").hide();
       $("#noOfBankDetails").change(function(){
           var value = $(this).val();

           if(value == 0) {
               $("#firstBankDetail").hide();
               $("#secondBankDetail").hide();
               $("#thirdBankDetail").hide();
               $("#fourthBankDetail").hide();
           }
           else if(value == 1) {
               $("#firstBankDetail").show();
               $("#secondBankDetail").hide();
               $("#thirdBankDetail").hide();
               $("#fourthBankDetail").hide();  
           }
           else if(value == 2) {
               $("#firstBankDetail").show();
               $("#secondBankDetail").show();
               $("#thirdBankDetail").hide();
               $("#fourthBankDetail").hide(); 
           }
           else if(value == 3){
               $("#firstBankDetail").show();
               $("#secondBankDetail").show();
               $("#thirdBankDetail").show();
               $("#fourthBankDetail").hide();  
           }
           else if(value == 4){
               $("#firstBankDetail").show();
               $("#secondBankDetail").show();
               $("#thirdBankDetail").show();
               $("#fourthBankDetail").show();
           }

        }); 

它显示了基于 bankDetails 的noOfBankDetails 下拉菜单。

接下来在提交时,我已经对可见应用了验证,bankDetail如图所示

   $('form').submit(function (event) {
       if($('#firstBankDetail').is(':visible')) {
           if(!validateFirstBankDetails()){
               event.preventDefault();
               return false;
           }
        } 

       if($('#secondBankDetail').is(':visible')) {
          if(!validateSecondBankDetails()){
           event.preventDefault();
           return false;
          }
        }   
       if($('#thirdBankDetail').is(':visible')) {
           if(!validateThirdBankDetails()){
               event.preventDefault();
               return false;
           }
        }   
       if($('#fourthBankDetail').is(':visible')) {
           if(!validateFourthBankDetails()){
               event.preventDefault();
               return false;
           }
        }
   });

这工作得很好。但是,当我将下拉列表选择为值 4 并在任何字段中的第四个详细信息中,我会填写预期为 double 的字符串,然后我再次选择下拉列表为 2。然后单击提交。在那种情况下,验证只运行两个,但是所有 4 个的数据都发送到服务器,我得到一个NumberFormatException.

任何解决方案???

编辑:包括

function validateFirstBankDetails(){
    var bankDetails0_nameOfBank = document.getElementById("bankDetails0_nameOfBank");
    var bankDetails0_nameOfBranch = document.getElementById("bankDetails0_nameOfBranch");
    var bankDetails0_accountNumber = document.getElementById("bankDetails0_accountNumber");
    var bankDetails0_accountType = document.getElementById("bankDetails0_accountType");

    var bankDetails0_EMIforProposedLoan = document.getElementById("bankDetails0_EMIforProposedLoan");

    if(bankDetails0_accountNumber.value == ""){
        alert("Please Enter the Account No");
        bankDetails0_accountNumber.setAttribute("style","background-color: #ffcccc");   
        return false;
    }

    if(bankDetails0_accountType.value == ""){
        alert("Please Select the Account Type");
        bankDetails0_accountType.setAttribute("style","background-color: #ffcccc");   
        return false;
    }

    if(bankDetails0_EMIforProposedLoan.value == ""){
        alert("Please Enter the EMI for proposed loan");
        bankDetails0_EMIforProposedLoan.setAttribute("style","background-color: #ffcccc");   
        bankDetails0_EMIforProposedLoan.focus();
        return false;
    }
    if(isNaN(bankDetails0_EMIforProposedLoan.value)){
        alert("Proposed EMI must be number only !");
        bankDetails0_EMIforProposedLoan.setAttribute("style","background-color: #ffcccc");   
        return false;
    }

    return true;
}
4

0 回答 0