使用原型获取/设置复选框和单选按钮值

发布于 2024-08-03 11:15:40 字数 150 浏览 2 评论 0原文

原型很棒,但是随着 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

∞琼窗梦回ˉ 2024-08-10 11:15:40

您始终可以执行以下操作:(假设您的复选框具有 checkboxes 类)。

var checkedList = [];
$('.checkboxes').each(function(ele){
   if( $(ele).checked )
   {
       checkedList.push($(ele).name);
   }
});

You can always do something like this: (assumes you have your checkboxes have a class of checkboxes).

var checkedList = [];
$('.checkboxes').each(function(ele){
   if( $(ele).checked )
   {
       checkedList.push($(ele).name);
   }
});
一个人的夜不怕黑 2024-08-10 11:15:40

编辑 - 刚刚意识到我误读了问题,下面的代码仅适用于设置值:

var form = $('options');
checkboxes = form.getInputs('checkbox');
checkboxes.each(function(e){ e.checked = 0 });
// or even checkboxes.invoke('checked',0);

不过也许可以使用类似的东西:

var cels = new Array();
$('checkbox[checked="checked"]').each(el){
  cels.push(el.value);
}

edit - just realised i misread the question, code below only good for setting values:

var form = $('options');
checkboxes = form.getInputs('checkbox');
checkboxes.each(function(e){ e.checked = 0 });
// or even checkboxes.invoke('checked',0);

could maybe use something like this though:

var cels = new Array();
$('checkbox[checked="checked"]').each(el){
  cels.push(el.value);
}
好听的两个字的网名 2024-08-10 11:15:40

这仅适用于单选按钮,但仍然有用。

获取

$('input:checked[name="radio_name"]')[0].value

设置

$('input[name="radio_name"][value="to_select"]')[0].checked = true

“radio_name”是您的广播组的名称。 “to_select”是您要选择的值。 “[0]”起作用是因为一次只能选中 1 个单选按钮。

顺便说一句,我有点不喜欢原型。在 jQuery 中要容易得多,但是你必须使用你必须使用的东西。

This is only for radio buttons, but still useful.

Get

$('input:checked[name="radio_name"]')[0].value

Set

$('input[name="radio_name"][value="to_select"]')[0].checked = true

"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.

时光与爱终年不遇 2024-08-10 11:15:40

我最终编写了自己的 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.

岁月静好 2024-08-10 11:15:40

给定 HTML:

<label><input type="radio" id="choiceA" name="choices" value="A" checked="checked" /> A</label>
<label><input type="radio" id="choiceB" name="choices" value="B" /> B</label>

这会执行 CSS 样式查询并返回所有元素的值。

$('input:checked[type="radio"][name="group-name"]').pluck("value");

Given HTML:

<label><input type="radio" id="choiceA" name="choices" value="A" checked="checked" /> A</label>
<label><input type="radio" id="choiceB" name="choices" value="B" /> B</label>

This does a CSS styles query and returns the value for all of the elements.

$('input:checked[type="radio"][name="group-name"]').pluck("value");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文