2010-11-04 144 views
1

我有一個表單驗證編碼問題。我將發佈下面的代碼。基本上我希望表單提交如果cboAction是在另一個選擇。幫助jQuery驗證

cboAction有三個值,[選擇]插入搜索。因此,如果在組合框中選擇了插入,那麼如果選擇了搜索,那麼表單應該使用jquery else進行驗證,然後表單應該被提交。我試圖在那裏放一個if語句,但它沒有奏效。 :(

者均基於表單驗證的作品,它只是我不能得到上述工作,希望從別人聽到。

感謝

jQuery.validator.addMethod(
      "selectOption", 
      function(value, element) { 
       if(element.value == "nothing") { 
        return false;      
       } else { 
        return true; 
       } 
      }, 
      "Please select an option." 
     ); 

     $(document).ready(function() {    
      $("#authorForm").validate({     
       rules: {               
         cboAction: {selectOption:false} 
         txtName: {required:true}, 
         txtStreet: {required:true} 
         txtTown: {required:true}, 
         cboState: {selectOption:true}, 
         txtPostcode: {required:true}, 
         txtMobile: {required:true}, 
         txtEmail: {required:true, email:true}        
       }, 
       messages: { 
        cboAction: "Please select an action"               
        txtName: {required: "Author's Name is a required field"}, 
        txtStreet: {required: "Street is a required field"}       
        txtTown: "Town is a required field", 
        cboState: "Please select a state", 
        txtPostcode: "Postcode is a required field", 
        txtMobile: 
        { 
         required: "Mobile is a required field", 
         email: "Please enter a valid email adress" 
        } 
       }   
      }); 
     }); 

回答

1

您必須手動觸發驗證:

http://docs.jquery.com/Plugins/Validation/Validator/form

首先防止驗證提交:

$("form").validate({ 
    onsubmit: false 
}); 

添加提交處理程序以測試cboAction。

$('form').submit(function(event){ 
    if ($('#cboAction').val() == 'insert' && !$('form').validate().form()){ 
     return false; 
    } 
    return true; 
}); 
+0

嗨,我把我的驗證碼之間的form.validate()。 – Mahie 2010-11-04 13:29:30

+0

是的,在提交之前提交:false:{... – 2010-11-04 13:50:22

+0

HALLELUJAH !!好極了。有效。謝謝哥們。 – Mahie 2010-11-04 14:39:18

0

這是使它工作的原因。

 jQuery.validator.addMethod(
      "selectOption", 
      function(value, element) { 
       if(element.value == "nothing") { 
        return false;      
       } else { 
        return true; 
       } 
      }, 
      "Please select an option." 
     ); 

     $(document).ready(function() {        

      $("#authorForm").submit(function(event){ 
       if($("#cboAction").val() == "insert" && !$("#authorForm").validate().form()){ 
        return false; 
       } 
       return true; 
      }); 

      $("#authorForm").validate({ 
       onsubmit: false, 
       rules: {               
         //cboAction: {selectOption:false}, 
         txtName: {required:true}, 
         txtStreet: {required:true}, 
         txtTown: {required:true}, 
         //cboState: {selectOption:true}, 
         txtPostcode: {required:true}, 
         txtMobile: {required:true}, 
         txtEmail: {required:true, email:true}        
       }, 
       messages: { 
        //cboAction: "Please select an action",             
        txtName: {required: "Author's Name is a required field"}, 
        txtStreet: {required: "Street is a required field"},      
        txtTown: "Town is a required field", 
        //cboState: "Please select a state", 
        txtPostcode: "Postcode is a required field", 
        txtMobile: "Mobile is a required field", 
        txtEmail: 
        { 
         required: "Email is a required field", 
         email: "Please enter a valid email adress" 
        } 
       }   
      }); 
     });