0

我在一个表单中有多个单选按钮。我正在验证那些提交表单的人。因此,如果未选择单选按钮,它将显示该单选按钮的标题属性中给出的错误。不同单选按钮的标题不同。这个显示错误现在工作正常。现在,当用户检查单选按钮时,我需要删除该错误消息。但这不起作用

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/

4

2 回答 2

0

写这个:

$('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();

在这里更新了小提琴。

于 2013-11-13T10:18:19.013 回答
0

你可以这样做:

$('.input-container .required').change(function () {
   $(this).closest('.input-container').next('ul.innererrormessages').remove();
});

小提琴演示

于 2013-11-13T10:20:28.570 回答