问题描述
我在页面上有这样一个单选按钮
从 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获取选定的单选按钮标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!