我们如何使用 DOM 访问单选按钮的值?
我们如何使用 DOM 访问单选按钮的值?
例如。 我们的单选按钮为:
<input name="sex" type="radio" value="male">
<input name="sex" type="radio" value="female">
它们位于名为 form1
的表单内。 当我尝试时,
document.getElementByName("sex").value
无论检查的值如何,它总是返回“男性”。
How can we access the value of a radio button using the DOM?
For eg. we have the radio button as :
<input name="sex" type="radio" value="male">
<input name="sex" type="radio" value="female">
They are inside a form with name form1
. When I try
document.getElementByName("sex").value
it returns 'male' always irrespective of the checked value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
只是为了“泛化” Canavar 非常有用的功能:
...现在将这样引用:
奇怪的是,这样的东西还没有成为 prototype.js 的一部分。
Just to "generify" Canavar's very helpful function:
... which would now be referenced thusly:
Strange that something like this isn't already a part of prototype.js.
令人惊讶的是,没有人建议实际使用 Selector API:
浏览器支持很好
Surprised no-one has suggested actually using the Selector API:
Browser support is good
如果您需要所选的一个,大多数框架都支持如下功能:
If you need the selected one, most frameworks support functionality like this:
您可以通过以下方法获取所选收音机的值:
You can get your selected radio's value by this method :
有几种方法。
1. 在每个输入上放置一个 id
然后你可以使用
document.getElementById("sex_male")
2. 使用类似 PrototypeJS 的东西(jQuery 也可以)
然后你可以这样做
:仅使用“male”单选按钮:
开始使用 Prototype 的一个简单方法是通过 Google 将其包含在内将其添加到 之间 您页面的标签:
There are a couple of ways.
1. Put an id on each input
Then you can use
document.getElementById("sex_male")
2. Use something like PrototypeJS (jQuery works too)
Then you can do something like this:
Or even this to get at just the "male" radio button:
An easy way to get started with Prototype is include it from Google by adding this between the <head></head> tags of your page:
如果您想要选定的一项,但没有方便的框架,您可以迭代元素数组,查找选中的一项:
If you want the selected one, but don't have a framework handy, you can iterate over the element array looking for whichever one is checked:
如果您使用
document.form1.sex
,则会返回一个数组。document.form1.sex[0]
= 第一个单选按钮document.form1.sex[1]
= 第二个单选按钮要检查哪个单选按钮被选中,您需要循环:
If you use
document.form1.sex
, you are returned an array.document.form1.sex[0]
= first radio buttondocument.form1.sex[1]
= second radio buttonTo check which is checked you need to loop:
您的意思是
getElementsByName('sex')[0].value
? (没有getElementByName
。)当然,这将始终选择具有该名称的第一个输入元素 - 其值确实为male 的元素。 然后,您可以使用“.checked”属性检查是否已选择它。
对于这个简单的情况,您可以逃脱:
对于一般情况,您必须循环每个无线电输入以查看哪个被检查。 首先获取表单对象可能会更好(在表单上放置一个 id 并使用
document.getElementById
通常比使用基于“名称”的 document.forms 集合更好),然后访问form.elements.sex 来获取列表,以防页面上有任何其他元素具有name="sex"
属性(可能是表单字段之外的其他元素)。You mean
getElementsByName('sex')[0].value
? (There's nogetElementByName
.)That will of course always pick the first input element with that name — the one whose value is indeed male. You then check to see if it's selected by using the ‘.checked’ property.
For this simple case you can get away with:
For the general case you have to loop over each radio input to see which is checked. It would probably be better to get the form object first (putting an id on the form and using
document.getElementById
is generally better than using the ‘name’-based document.forms collection), and then access form.elements.sex to get the list, in case there are any other elements on the page that havename="sex"
attributes (potentially other things than form fields).正如其他人所展示的那样,循环可以完成该任务,但它可能比使用循环更简单,如果需要,这将有助于提高性能。 此外,它可以是便携式/模块化的,因此可以用于不同的无线电组。
简单的例子
一个更复杂的例子:
Loops can achieve the task, as others have shown but it could be simpler than using a loop, which will help performance if desired. Also, it can be portable/modular so it can be used for different radio groups.
Simple Example
A more complex example: