获取属于选中单选按钮的标签文本 [英] get text of label that belongs to checked radio button

查看:66
本文介绍了获取属于选中单选按钮的标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取属于checked单选按钮的标签的text,但似乎不太能够.

I'm trying to get the text of the labels that belong to checked radio buttons, but don't seem quite to be able to.

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

        });
      }
    });
  });
});

jsbin

出于任何原因打印出

- [11:07:13.834]({0:({}),上下文:({}),长度:1})@ http://jsbin.com/uniwum/2/edit:71

-- [11:07:13.834] ({0:({}), context:({}), length:1}) @ http://jsbin.com/uniwum/2/edit:71

在控制台中

.我做错了什么?

in the console. What have I done wrong?

推荐答案

这样做:-

$('#btn').click(function() {
    $("input[type='radio']:checked").each(function() {
        var idVal = $(this).attr("id");
        alert($("label[for='"+idVal+"']").text());
    });
});

引用 实时演示

Refer LIVE DEMO

下面是文档参考:-

这篇关于获取属于选中单选按钮的标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆