如何使用jQuery获取选定的单选按钮标签文本 [英] How to get the selected radio button label text using jQuery
问题描述
我在页面上有这样一个单选按钮
< div id =someId>
< label class =radio-inline>
< input name =xtype =radioonchange =GetSelectedVal();>是< / label>
< label class =radio-inline>
< input name =xtype =radioonchange =GetSelectedVal();>否< / label>
< / div>
在页面加载时,我不想设置任何选择,因此不使用选中
属性。在我的JavaScript函数中,如何根据用户在运行时的选择来获取值是
或否
p>
函数GetSelectedVal(){
console.log($('#someId input:radio ....... ..);
}
我已经看到类似的问题,但无法找到解决方案。
从 onchange
内联处理程序> HTML 。在上使用来绑定事件。
:checked
将选择选中
单选按钮。最接近
将选择父 label
和 text()
将会得到与单选按钮相关的标签,例如是
$ b $($'$ $ $'$'$'$'$'$'$'$'$'$'$'$'$' ('label')。text());
});
I have a radio button like this on page
<div id="someId">
<label class="radio-inline">
<input name="x" type="radio" onchange="GetSelectedVal();">Yes</label>
<label class="radio-inline">
<input name="x" type="radio" onchange="GetSelectedVal();">No</label>
</div>
On page load I don't want to set any selection so not using checked
property. In my JavaScript function, how can I get the value Yes
or No
based on the user selection at runtime?
function GetSelectedVal() {
console.log($('#someId input:radio.........);
}
I have seen similar questions but unable to find solution of this issue.
Remove onchange
inline handler from HTML
. Use on
to bind events.
:checked
will select the checked
radio button. closest
will select the parent label
and text()
will get the label associated with the radio button. e.g. Yes
$('[name="x"]').on('change', function () {
alert($('[name="x"]:checked').closest('label').text());
});
这篇关于如何使用jQuery获取选定的单选按钮标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!