2012-11-02 40 views
0

我試圖根據在選擇框中選擇的內容來激活/取消激活驗證。激活/取消選擇框選項上的Spry驗證

這是可選的添加地址,如果地址被選中,國家&區域字段被顯示,並且spry_country,spry_region將需要驗證。

價格是可選的,如果添加價格&價格稅務字段顯示,spry_price & spry_price_tax將需要驗證等等。

我有下面的代碼似乎工作正常,但是,如果您選擇地址,然後改變主意,然後決定不輸入地址代碼將隱藏字段,但似乎仍然嘗試和驗證隱藏字段爲我無法提交表單。希望這是有道理:)

/* If address selected */ 
$("#location_option").change(function() { 
    if ($("#location_option").val() == "address"){ 
     $(".show_location").show(); 
     /* activate spry */ 
     var spryselect4 = new Spry.Widget.ValidationSelect("spry_country", {isRequired:true}); 
     var spryselect5 = new Spry.Widget.ValidationSelect("spry_region", {isRequired:true}); 
    } 
    else{ 
     $(".show_location").hide(); 
     var spryselect4 = new Spry.Widget.ValidationSelect("spry_country", {isRequired:false}); 
     var spryselect5 = new Spry.Widget.ValidationSelect("spry_region", {isRequired:false}); 
    } 
}); 
$("#location_option").change(); 

/* If price selected */ 
$("#price_option").change(function() { 
    if ($("#price_option").val() == "price"){ 
     $(".show_price").show(); 
     /* activate spry */ 
     var sprytextfield2 = new Spry.Widget.ValidationTextField("spry_price", "none", {isRequired:true}); 
     var spryselect7 = new Spry.Widget.ValidationSelect("spry_price_tax", {isRequired:true}); 
    } 
    else{ 
     $(".show_price").hide(); 
     var sprytextfield2 = new Spry.Widget.ValidationTextField("spry_price", "none", {isRequired:false}); 
     var spryselect7 = new Spry.Widget.ValidationSelect("spry_price_tax", {isRequired:false}); 
    } 
}); 
$("#price_option").change(); 

/* If date selected */ 
$("#date_option").change(function() { 
    if ($("#date_option").val() == "date"){ 
     $(".show_date").show(); 
     /* activate spry */ 
     var sprytextfield3 = new Spry.Widget.ValidationTextField("spry_start_date", "none", {isRequired:true}); 
     var sprytextfield4 = new Spry.Widget.ValidationTextField("spry_finish_date", "none", {isRequired:true}); 
    } 
    else{ 
     $(".show_date").hide(); 
     var sprytextfield3 = new Spry.Widget.ValidationTextField("spry_start_date", "none", {isRequired:false}); 
     var sprytextfield4 = new Spry.Widget.ValidationTextField("spry_finish_date", "none", {isRequired:false}); 
    } 
}); 
$("#date_option").change(); 

回答

0

我已經用它進行排序:

spryselect4 = new Spry.Widget.Utils.destroyWidgets("spry_country"); 
1

,而不是這樣做:

var spryselect4 = new Spry.Widget.ValidationSelect("spry_country", {isRequired:false}); 

地說:

spryselect4.reset(); // remove the error message 
spryselect4.destroy(); // remove the validation 
+0

感謝隊友,儘管如此,似乎沒有任何瑕疵。 – monsterboy

+0

你確定嗎?因爲我嘗試了你的建議並沒有發生任何事情。 – Gustonez

+0

我測試了它在Firefox,鉻和即不同的組合,它似乎工作正常。 – monsterboy