2014-01-17 68 views
5

我有一個問題,使用引導選擇插件和jQuery驗證。當我選擇值時,錯誤消息This field is required不能刪除而選擇值錯誤信息自動刪除後正常驗證(無引導選擇插件)。如何解決這個問題?選擇值後刪除jquery驗證錯誤消息

JSFIDDLE

HTML:

<form id="myform"> <----With Select Plugin 
    <select name="year" class="selectpicker"> 
     <option value="">Year</option> 
     <option value="1">1955</option> 
     <option value="2">1956</option> 
    </select> 
    <br/> 
    <input type="submit" /> 
</form> 
<form id="myform1"> <----Without Select Plugin 
    <select name="fruit"> 
     <option value="">Year</option> 
     <option value="1">1955</option> 
     <option value="2">1956</option> 
    </select> 
    <br/> 
    <input type="submit" /> 
</form> 

JS:

$(document).ready(function() { 
    $('.selectpicker').selectpicker(); 


    $('#myform').validate({ // initialize the plugin 
     ignore: [], 
     rules: { 
      year: { 
       required: true 
      } 
     }, 
     errorPlacement: function(error, element) { 
      if (element.attr("name") == "year") { 
       error.insertAfter(".bootstrap-select"); 
      } else { 
       error.insertAfter(element); 
      } 
     }, 
     submitHandler: function (form) { // for demo 
      alert('valid form submitted'); // for demo 
      return false; // for demo 
     } 
    }); 
}); 
// just for the demos, avoids form submit 
jQuery.validator.setDefaults({ 
debug: true, 
success: "valid" 
}); 
$("#myform1").validate({ 
rules: { 
fruit: { 
required: true 
} 
} 
}); 

回答

7

您需要檢查變化的有效性

$('.selectpicker').selectpicker().change(function(){ 
    $(this).valid() 
}); 

演示:Fiddle

0

試試這個

$('select').change(function(){ 
    if ($(this).val()!="") 
    { 
     $(this).valid(); 
    } 
}); 

這裏是Fiddle