7

如标题中所述:

<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>

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

4

5 回答 5

18

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


更新:

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

$('input:radio:checked').next('label:first').html()
于 2010-01-25T13:08:13.410 回答
7

这个怎么样?

var forLabel = $('input:radio:checked').attr("id");
$("label[for='" + forLabel + "']").text();
于 2010-01-25T13:13:32.173 回答
3

采用.next();

$("input:radio:checked").next().text();
于 2010-01-25T13:16:39.857 回答
2

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

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

用于此的 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>
于 2014-02-11T18:41:10.043 回答
1

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

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

这是一个工作演示

于 2010-01-27T13:41:53.470 回答