admin管理员组文章数量:1332395
<label for="radio1"> This Radio Button 1 has a label associated with it.</label> <input type="radio" name="Radio" id="radio1" />
above code has a label and radio button , the label is associated with radio button using the for attribute.
I would like to get the label text if user selects/checks the associated radio button.
<label for="radio1"> This Radio Button 1 has a label associated with it.</label> <input type="radio" name="Radio" id="radio1" />
above code has a label and radio button , the label is associated with radio button using the for attribute.
I would like to get the label text if user selects/checks the associated radio button.
Share Improve this question asked Apr 6, 2011 at 10:01 SandhurstSandhurst 1,1805 gold badges26 silver badges41 bronze badges5 Answers
Reset to default 3I know this question is months old, and even already has an accepted answer, but every response here uses the prev
method. Just wanted to point out that a slightly more robust technique would be using an attribute selector:
$('input:radio').click(function () {
var txt = $('label[for=' + this.id + ']').text();
alert(txt);
});
If your using jquery:
$('input[name="Radio"]').click(function(){ alert( $(this).prev('label').text() ) });
check out this example: http://jsfiddle/7FzQ9/
$('input#radio1').prev('label[for=radio1]').text();
$("input:radio").click(function() {
// if the label is before the radio button
alert( $(this).prev("label").text() );
// otherwise use
alert( $("label[for='" + this.id + "']").text() );
});
You can also use:
$("input:radio").click(function() {
var label_description = this.parentElement.outerText;
alert( label_description );
} )
Js Fiddle test:
Get the label for a radio
本文标签: get associated radio button label text using javascriptStack Overflow
版权声明:本文标题:get associated radio button label text using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742260497a2442422.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论