我如何知道通过 jQuery 选择了哪个单选按钮?
我有两个单选按钮,想要发布所选按钮的值。 如何使用 jQuery 获取值?
我可以这样获取所有这些:
$("form :radio")
我如何知道选择了哪一个?
I have two radio buttons and want to post the value of the selected one.
How can I get the value with jQuery?
I can get all of them like this:
$("form :radio")
How do I know which one is selected?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
要获取 id 为
myForm
的表单的 selectedradioName
项的值:以下是一个示例:
To get the value of the selected
radioName
item of a form with idmyForm
:Here's an example:
用这个..
Use this..
如果您已经引用了单选按钮组,例如:
使用
filter()
函数,而不是find()
。 (find()
用于定位子/后代元素,而filter()
用于搜索选择中的顶级元素。)注释:答案最初是纠正另一个建议使用 find() 的答案,该答案似乎已被更改。
find()
对于已经引用了容器元素但没有引用单选按钮的情况仍然有用,例如:If you already have a reference to a radio button group, for example:
Use the
filter()
function, notfind()
. (find()
is for locating child/descendant elements, whereasfilter()
searches top-level elements in your selection.)Notes: This answer was originally correcting another answer that recommended using
find()
, which seems to have since been changed.find()
could still be useful for the situation where you already had a reference to a container element, but not to the radio buttons, e.g.:这应该有效:
注意 input:checked 周围使用的“”,而不是像 Peter J 的解决方案那样的“”
This should work:
Note the "" usaged around the input:checked and not '' like the Peter J's solution
您可以将 :checked 选择器与单选选择器一起使用。
You can use the :checked selector along with the radio selector.
如果您只想要布尔值,即是否选中它,请尝试以下操作:
If you want just the boolean value, i.e. if it's checked or not try this:
获取所有无线电:
过滤以获取选中的无线电
Get all radios:
Filter to get the one thats checked
另一种选择是:
Another option is:
就我而言,我在一种表单中有两个单选按钮,我想知道每个按钮的状态。
下面这对我有用:
In my case I have two radio buttons in one form and I wanted to know the status of each button.
This below worked for me:
以下是我编写表格并处理获取已检查收音机的方法。
使用名为 myForm 的表单:
从表单中获取值:
如果您不发布表单,我将通过使用进一步简化它:
然后获取选中的值变为:
在输入上有一个类名可以让我轻松地设置样式输入...
Here's how I would write the form and handle the getting of the checked radio.
Using a form called myForm:
Get the value from the form:
If you're not posting the form, I would simplify it further by using:
Then getting the checked value becomes:
Having a class name on the input allows me to easily style the inputs...
试试这个。
这对我有用
try this one.
it worked for me
在 JSF 生成的单选按钮(使用
标记)中,您可以执行以下操作:其中 selectOneRadio ID 为 radiobutton_id 并形成ID 为 form_id。
请务必使用 name 而不是 id,如图所示,因为 jQuery 使用此属性(name 由 JSF 自动生成,类似于控件 ID)。
In a JSF generated radio button (using
<h:selectOneRadio>
tag), you can do this:where selectOneRadio ID is radiobutton_id and form ID is form_id.
Be sure to use name instead id, as indicated, because jQuery uses this attribute (name is generated automatically by JSF resembling control ID).
另外,检查用户是否没有选择任何内容。
Also, check if the user does not select anything.
如果您有单一形式的多个单选按钮,那么
这对我有用。
If you have Multiple radio buttons in single form then
This is working for me.
我编写了一个 jQuery 插件来设置和获取单选按钮值。 它还尊重他们身上的“改变”事件。
将代码放在任何地方以启用插件。 然后享受吧! 它只是覆盖默认的 val 函数而不会破坏任何东西。
您可以访问这个 jsFiddle 来尝试它,看看它是如何工作的。
小提琴
I wrote a jQuery plugin for setting and getting radio-button values. It also respects the "change" event on them.
Put the code anywhere to enable the addin. Then enjoy! It just overrides the default val function without breaking anything.
You can visit this jsFiddle to try it in action, and see how it works.
Fiddle
这工作正常
工作演示
:checked
选择器适用于复选框
、单选按钮
和选择元素。 仅对于选择元素,请使用:selected
选择器。:checked 选择器的 API
This works fine
Working Demo
The
:checked
selector works forcheckboxes
,radio buttons
, and select elements. For select elements only, use the:selected
selector.API for
:checked Selector
要获取使用类的选定无线电的值:
To get the value of the selected radio that uses a class:
我使用这个简单的脚本
I use this simple script
用这个:
Use this:
演示:https://jsfiddle.net/ipsjolly/xygr065w/
DEMO : https://jsfiddle.net/ipsjolly/xygr065w/
如果 1 个表单上只有 1 组单选按钮,则 jQuery 代码如下所示:
If you only have 1 set of radio buttons on 1 form, the jQuery code is as simple as this:
使用
$('input[name="radioName"]').filter(":checked").val();
对我来说是最好的方法。测试过,这可以工作
表单
Jquery
示例如下:
https://codepen.io/abinhho/pen/mdLrqbX
Using
$('input[name="radioName"]').filter(":checked").val();
is the best way for me.Tested, this works
Form
Jquery
Example here:
https://codepen.io/abinhho/pen/mdLrqbX
我已经发布了一个库来帮助解决这个问题。 实际上,提取所有可能的输入值,但还包括选中了哪个单选按钮。 您可以在 https://github.com/mazondo/formalizedata 查看它,
它会给您一个js 对象的答案,所以像这样的形式:
会给你:
I've released a library to help with this. Pulls all possible input values, actually, but also includes which radio button was checked. You can check it out at https://github.com/mazondo/formalizedata
It'll give you a js object of the answers, so a form like:
will give you:
JQuery 获取表单中的所有单选按钮和选中的值。
JQuery to get all the radio buttons in the form and the checked value.
要检索 JavaScript 数组中的所有单选按钮值,请使用以下 jQuery 代码:
To retrieve all radio buttons values in JavaScript array use following jQuery code :
尝试一下-
try it-
另一种获取方式:
Another way to get it:
从这个问题中,我想出了另一种方法来访问当前选定的
输入
,当您位于其各自标签的click
事件中。 原因是新选择的input
直到其label
的点击事件之后才会更新。TL;DR
From this question, I came up with an alternate way to access the currently selected
input
when you're within aclick
event for its respective label. The reason why is because the newly selectedinput
isn't updated until after itslabel
's click event.TL;DR