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


アップデート:

コメントセクションでビクターが指摘したように、前のセレクターは常に最初のラベルを選択します。次の関数が機能するはずです。

$('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

次のAdjacent Selector+を使用するのはどうですか?

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

これが実用的なデモです

于 2010-01-27T13:41:53.470 に答える