使用原型获取/设置复选框和单选按钮值
原型很棒,但是随着 1.6.1 版本的发布,该库仍然不允许获取/设置分组输入(复选框和单选按钮)。我想要一种使用 $ 获取选定值数组的方法F($("form").checkboxes)
.另一方面,我希望能够将这些复选框设置为值数组。
有想法吗?
Prototype is great, but with the 1.6.1 release, the library still doesn't allow getting/setting of grouped inputs (checkboxes and radio buttons.) I'd like a way to get an array of selected values with $F($("form").checkboxes)
. I'd like to be able to set those checkboxes to an array of values, on the flip side.
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您始终可以执行以下操作:(假设您的复选框具有
checkboxes
类)。You can always do something like this: (assumes you have your checkboxes have a class of
checkboxes
).编辑 - 刚刚意识到我误读了问题,下面的代码仅适用于设置值:
不过也许可以使用类似的东西:
edit - just realised i misread the question, code below only good for setting values:
could maybe use something like this though:
这仅适用于单选按钮,但仍然有用。
获取
设置
“radio_name”是您的广播组的名称。 “to_select”是您要选择的值。 “[0]”起作用是因为一次只能选中 1 个单选按钮。
顺便说一句,我有点不喜欢原型。在 jQuery 中要容易得多,但是你必须使用你必须使用的东西。
This is only for radio buttons, but still useful.
Get
Set
"radio_name" is the name of your radio group. "to_select" is whichever value you want to select. The "[0]" works because only 1 radio button can be checked at a time.
On a side note, I kinda don't like prototype. A lot easier in jQuery, but you gotta use what you gotta use.
我最终编写了自己的 Prototype 扩展来做到这一点。您可以在 Github 上查看它们。以下是该项目的注释:
“这些扩展允许您使用 Prototype 方便的 $F() 语法来获取和设置这些分组输入元素的值。它们已经使用 Prototype 1.6.0.3 和 1.6.1 进行了测试。有这些扩展中的其他部分也请参阅 README.html 文件了解更多信息。细节。”
非常感谢 sawgee 和 Los 的答案,但我想要一个更集成的解决方案,它允许我使用我已经与其他表单元素一起使用的自然 $F() 语法来处理复选框和单选按钮。
I ended up writing my own extensions to Prototype to do this. You can see them on Github. Here's a note from the project:
"These extensions allow you to use Prototype’s convenient $F() syntax to get and set the values of these grouped input elements. They’ve been tested with Prototype 1.6.0.3 and 1.6.1. There are other bits in these extensions as well. See the README.html file for more details."
The answers from seengee and Los are much appreciated, but I wanted a more integrated solution that would allow me to work with checkboxes and radio buttons with the natural $F() syntax that I already use with other form elements.
给定 HTML:
这会执行 CSS 样式查询并返回所有元素的值。
Given HTML:
This does a CSS styles query and returns the value for all of the elements.