我正在尝试获取text属于checked单选按钮的标签,但似乎无法完成.
$(document).ready(function(){
$("body").on("click", "#btn", function(){
var radios = $("input[type='radio']");
$.each(radios, function(index, element){
if ($(this).prop("checked") === true){
var radioID = element.id;
$("label").each(function(ind, elm){
if ($(elm).prop("for") == radioID){
console.log($(elm));
}
});
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
无论出于什么原因打印出来
- [11:07:13.834]({0:({}),上下文:({}),篇幅:1})@ http://jsbin.com/uniwum/2/edit:71
在控制台中.我做错了什么?
Siv*_*ran 33
这样做: -
$('#btn').click(function() {
$("input[type='radio']:checked").each(function() {
var idVal = $(this).attr("id");
alert($("label[for='"+idVal+"']").text());
});
});
Run Code Online (Sandbox Code Playgroud)
参考现场演示
以下是文档参考: -