javascript - 我想使用 radio 输入的文本。我如何使用 jQuery 或 JavaScript 获取它

标签 javascript html jquery radio-button

我想使用如下所示的文本 5 和 6。我无法修改 html,因为它是自动生成的,并且还有许多其他具有相同结构的 radio 。我如何使用 jQuery 或 JavaScript 来实现这一点并将它们存储为变量。

<label class="radio">
    <label style="display: none;">
        <input type="radio" name="x_keeper1_price" id="x_keeper1_price_0" value="21"/>
    </label>
    5
</label>

<label class="radio">
    <label style="display: none;">
        <label style="display: none;">
            <input type="radio" name="x_Defd5_price" id="x_Defd5_price_0" value="28"/>
        </label>
    </label>
    6
</label>

最佳答案

如果单选按钮后的文本是 html 代码,这是一个更好的解决方案:

var text = $("#x_keeper1_price_0") //the radio button
           .closest("label.radio") //the label with class=radio)
           .html()                 //the html code
           .replace(/<label[\d\D]+<\/label>/, '');
           /* Removing the "second" label and its content.
              That will get the text after the radio button. */
alert(text);

jsfiddle:http://jsfiddle.net/72KcV/3/

关于javascript - 我想使用 radio 输入的文本。我如何使用 jQuery 或 JavaScript 获取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22882214/

相关文章:

javascript - 来自服务器的 GMT 时间倒计时

html - 将 <TD> 的部分内容相互对齐

javascript - 提交嵌套表单,奇怪的行为

javascript - 从javascript链接到url?

javascript - Php Counter 在加载后隐藏逗号并且不会永久显示

javascript - 我们可以绘制同时包含实线和虚线的折线图吗?

javascript - 简单的iFrame不显示一些跨域页面

jquery - fullCalendar json 与 php 在 "agendaWeek"

javascript - Div 翻转重叠问题

javascript - Node websockets/ws - WSS : How to tell if I closed the connection, 或者他们做到了?