2013-08-27 50 views
1

使用此jQuery代码:获取属性名称,而不是价值 - 查询

$("#targeting input:radio").each(function() { 
    console.log ($(this).attr("selected")); 
}); 

HTML:

<input type="radio" id="dateFromTo" name="muScheduleDateRange" value="2" selected="someval"/> 

在执行console.log我越来越selected代替someval

+0

您需要提到的meagars回答了'value'设置为 'Someval',而不是'selected'。 – putvande

回答

7

selected是一个布尔值。除undefined"selected"之外,您不能将其设置为任何内容。如果您想为input添加一些值,请使用value属性。如果要将某些附加值附加到input,请使用数据属性。

0

试试这个

console.log($('#targeting input:radio:checked').val()) 

jquery val

0

试试这个:

console.log($(this).prop('checked')); 

将返回true或false,这取决于单选按钮是否被选中。

Source