jQuery 如何获取电台组中所选电台的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2132407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:50:40  来源:igfitidea点击:

How Can I get the text of the selected radio in the radio groups

jquerytextradio-buttonchecked

提问by Michael

as said in the title for example:

如标题所述,例如:

<input id="User_Type_0" type="radio" name="User_Type" value="1" checked="checked" />
<label for="User_Type_0">User1</label>
<input id="User_Type_1" type="radio" name="User_Type" value="2" />
<label for="User_Type_1">User2</label>

how can I get the text:User 1

我怎样才能得到文本:用户 1

回答by Darin Dimitrov

$('input:radio:checked').siblings('label:first').html()

$('input:radio:checked').siblings('label:first').html()



UPDATE:

更新:

As pointed out by Victor in the comments section the previous selector will always select the first label. The nextfunction should work:

正如 Victor 在评论部分指出的那样,前一个选择器将始终选择第一个标签。在接下来的功能应该工作:

$('input:radio:checked').next('label:first').html()

回答by Natrium

how about this?

这个怎么样?

var forLabel = $('input:radio:checked').attr("id");
$("label[for='" + forLabel + "']").text();

回答by Reigel

use .next();

.next();

$("input:radio:checked").next().text();

回答by Paul Vargas

This works for me (I'm was using jQuery Mobile):

这对我有用(我正在使用 jQuery Mobile):

var value = $(":radio[name=location]:checked").val();
var text = $(":radio[name=location]:checked").prev("label").text();

The DOM for this:

用于此的 DOM:

<div id="locations" data-role="controlgroup" class="ui-controlgroup ui-controlgroup-vertical ui-corner-all">
   <div class="ui-controlgroup-controls ">
      <div class="ui-radio">
         <label for="location0" class="ui-btn ui-corner-all ui-btn-inherit ui-btn-icon-left ui-radio-off ui-first-child">1439</label>
         <input type="radio" name="location" id="location0" value="1439">
      </div>
      <div class="ui-radio">
         <label for="location1" class="ui-btn ui-corner-all ui-btn-inherit ui-btn-icon-left ui-radio-off ui-last-child">1440</label>
         <input type="radio" name="location" id="location1" value="1440">
      </div>
   </div>
</div>

回答by Russ Cam

What about using the next Adjacent Selector, +?

使用下一个相邻选择器+怎么样?

$('input:radio:checked + label').text();

Here's a Working Demo

这是一个工作演示