2010-08-06 71 views
-1

我有我的問題的一個簡化版本在這裏: http://www.arianhojat.com/temp/jquery/dynamic_validate/test.html刪除動態驗證與jQuery驗證插件

基本上我有一個郵件規則,基於一個複選框我刪除它...但它仍然是不被刪除...我嘗試將它移動到自己的規則與「添加」,然後試圖刪除通過其隨附的「刪除」功能,但仍然是同樣的問題。

所以當你填寫你的名字後,嘗試提交。 它將錯誤的電子郵件字段... 然後檢查複選框,它應該刪除#email的驗證,它只能刪除它的一半。就像如果你嘗試用名字提交,它允許它......但現在在電子郵件字段中輸入一些內容並嘗試提交...它會錯誤地說它無效(但驗證已被刪除?)

謝謝!

代碼是在這裏以供參考:

$(document).ready(function(){ 


    $("#contactForm").validate({ 
onfocusout: false, 
debug: true, 

    rules: {  
     fname: { 
     required: true 
     } 
    }, 

    messages: {    
     fname: { 
     required: "* Please enter a first name" 
     } 
    }, 

    errorLabelContainer: "#messageBox", 
    wrapper: "li", 
    submitHandler: function(form) {    

     alert('Submitted'); 
     return false; 
    } 
    }); 

    $("#email").rules("add", { 
    required: true, 
    email: true, 
    messages: { 
     required: "* Please enter a sweet email address", 
     email: "* Please enter a sweet valid email address" 
    } 
    }); 

    $('#no_email').bind('change', function(event) { 

    if($(this).attr('checked')) { 
console.log('removed?'); 

     $("#email").rules("remove"); //, "required email" ... still doesnt work if specify the rules to remove 

    } else { 

console.log('re-added?'); 
     $("#email").rules("add", { 
     required: true, 
     email: true, 
     messages: { 
      required: "* Please enter an awesome email address", 
      email: "* Please enter an awesome valid email address" 
     } 
     }); 

    } //else 


    $("#contactForm").validate().element("#email"); 



    }); 


}); 
.... 

<form id="contactForm" action="doesnt_exist_yet2.html" method="post"> 

    <ul id="messageBox"></ul> 

    <div style=""> 
    <p>First name:</p> 
    <input type="text" name="fname" id="fname" class="fname" maxlength="100"/> 
    </div> 


    <div style=""> 
    <p>Your Email Address</p> 
    <input type="text" name="email" id="email" class="email" maxlength="100"/> 
    </div> 


    <br/><br/> 
    <input type="checkbox" name="no_email" id="no_email" /> Turn off validation for EMAIL 




    <input type="submit" name="submit_btn" id="submit_btn" value="Submit"> 
</form> 

回答

1

我想我解決它,不能有#email和郵件規則。我將我的字段更改爲#myemail並認爲它現在可用。 errrrr!