0

我在表单中有多个单选按钮。我想在提交页面时验证那些。在提交页面时,如果未选中单选按钮,我需要显示该单选按钮的标题属性中给出的错误消息。不同单选按钮的标题不同。如何获取未选中的单选按钮的标题属性?

HTML

 <div class="input-container" data-validation="required">
<input type="radio" name="radio1" id="first" value="First" class="required "title="Please select to continue."/>
<label for="first">First</label>
</div>

jQuery

$(function() {
$('button').click(function(){
$.each($('.input-container[data-validation=required]'), function (idx,group) {
        var current = $(group).find('[type=radio]:checked').val();
        alert(current);
        if( current === undefined ) {
            //I need to display the title as the error message
            //like var title = current.attr('title');
            $(group).after('<ul class="innererrormessages"><li>'+title+'</li></ul>');
        }


    });
});
});

请找到我的html的小提琴

http://jsfiddle.net/jUQYr/15/

任何帮助是极大的赞赏。

4

2 回答 2

1

提琴手

$(function() {
$('button').click(function(){
$.each($('.input-container[data-validation=required]'), function (idx,group) {
        var checked = $(group).find('[type=radio]:checked');
        var unchecked = $(group).find('[type=radio]');

        if(checked.length != unchecked.length ) {
            $.each(unchecked, function(i,v) {
                if($(v).attr('id') != $(checked[i]).attr('id')) {
                    //I need to display the title as the error message
                    var title = $(v).attr('title');
                    $(group).after('<ul class="innererrormessages"><li>'+title+'</li></ul>');    
                }
            });
        }
    });
});
});
于 2013-11-13T08:28:23.783 回答
0

现场演示

$('button').click(function(){

   $.each($('.input-container[data-validation=required]'), function (idx, el) {
       var current = $(el).find(':radio');
       if( !current.is(':checked') ) {
            var title = current.attr('title');
            $(el).after('<ul class="innererrormessages"><li>'+title+'</li></ul>');
       }
    });

});
于 2013-11-13T08:16:13.877 回答