如何使用jQuery获取选定的单选按钮标签文本 [英] How to get the selected radio button label text using jQuery

查看:76
本文介绍了如何使用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());

});

DEMO


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());

});

DEMO

这篇关于如何使用jQuery获取选定的单选按钮标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆