У меня есть несколько переключателей в форме. Я проверяю их на отправке формы. Поэтому, если радиокнопки не выбраны, отобразится ошибка, указанная в атрибуте title этого переключателя. Название отличается для разных переключателей. Эта ошибка отображения теперь работает нормально. Теперь мне нужно удалить это сообщение об ошибке, когда пользователь проверяет переключатель. Но это не работает
HTML
<div class="input-container" data-validation="required">
<input type="radio" name="radio1" id="first" value="First" class="required "title="Please select first to continue."/>
<label for="first">First</label>
<br/>
<input type="radio" name="radio1" id="second" value="Second" class="required "title="Please select first to continue."/>
<label for="second">Second</label>
</div>
<hr/>
<div class="input-container" data-validation="required">
<input type="radio" name="radio2" id="first" value="First" class="required "title="Please select second to continue."/>
<label for="first">First</label>
<br/>
<input type="radio" name="radio2" id="second" value="Second" class="required "title="Please select second to continue."/>
<label for="second">Second</label>
</div>
<hr/>
<button>Validate</button>
Javascript
$(function() {
$('button').click(function(){
$.each($('.input-container[data-validation=required]'), function (idx,group) {
var current = $(group).find(':radio');
if( !current.is(':checked') ) {
var title = current.attr('title');
$(group).next('ul.innererrormessages').remove();
$(group).after('<ul class="innererrormessages"><li>'+title+'</li></ul>');
}
});
});
$('.input-container .required').change(function(){
$(this).next('ul.innererrormessages').remove();
});
});
Скрипка http://jsfiddle.net/jUQYr/26/
Вы можете сделать это:
$('.input-container .required').change(function () {
$(this).closest('.input-container').next('ul.innererrormessages').remove();
});
Напишите это:
$('button').click(function(){
$(".innererrormessages").remove(); // add this line to clear messages first
$.each($('.input-container[data-validation=required]'), function......
Меняться от
$(this).next('ul.innererrormessages').remove();
в
$(this).parents(".input-container").next('ul.innererrormessages').remove();