获取未在jquery中检查的单选按钮的title属性

时间:2013-11-13 07:50:41

标签: jquery

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

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/

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

JsFiddle

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

答案 1 :(得分:0)

<强> Live demo

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

});