如何获取收音机组中所选收音机的文本

发布于 2024-08-19 02:30:09 字数 334 浏览 7 评论 0原文

正如标题所说 例如:

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

我怎样才能获取文本:User 1

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡莣 2024-08-26 02:30:10

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


更新:

正如 Victor 在评论部分中指出的那样,前一个选择器将始终选择第一个标签。 next 函数应该可以工作:

$('input:radio:checked').next('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 next function should work:

$('input:radio:checked').next('label:first').html()
许仙没带伞 2024-08-26 02:30:10

这个怎么样?

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

how about this?

var forLabel = $('input:radio:checked').attr("id");
$("label[for='" + forLabel + "']").text();
友欢 2024-08-26 02:30:10

使用 .next();

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

use .next();

$("input:radio:checked").next().text();
九歌凝 2024-08-26 02:30:10

这对我有用(我正在使用 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>

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

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

The DOM for this:

<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>
墨小沫ゞ 2024-08-26 02:30:10

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

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

这是一个工作演示

What about using the next Adjacent Selector, +?

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

Here's a Working Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文