Javascript Mootools - 如何从其名称中获取所选无线电输入类型的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2391618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mootools - how to get value of selected radio input type, from its name
提问by philfreo
Question 1: Given
问题 1:给定
<input type="radio" name="foo" value="1" />
<input type="radio" name="foo" value="2" />
<input type="radio" name="foo" value="3" />
In Mootools, how do I return "2" given an input of "foo", assuming that the second radio button was clicked.
在 Mootools 中,假设单击了第二个单选按钮,我如何在输入“foo”的情况下返回“2”。
Question 2: (it's related)- Given similar checkboxinputs, how do I return either an array or comma separated list of the checked values?
问题 2:(相关)- 给定类似的复选框输入,如何返回已检查值的数组或逗号分隔列表?
I'm wanting to submit the values of these inputs via Request.JSON, passing it as a GET parameter.
我想通过 Request.JSON 提交这些输入的值,将其作为 GET 参数传递。
回答by Anurag
Assuming the name of the checkbox is "foo", to get the selected radio item, you can use:
假设复选框的名称是“foo”,要获取选中的单选项,您可以使用:
var values = $$('input[name=foo]:checked'??????????????).map(function(e) { return e.value; });
This returns an array with 1 item, being the selected elements' value.
这将返回一个包含 1 个项目的数组,即所选元素的值。
Or just extend an Array's prototype, and add a getFirst() method.
或者只是扩展一个数组的原型,并添加一个 getFirst() 方法。
Array.implement({
getFirst: function() {
if(this.length == 0) {
return null;
}
return this[0];
}
});
Then you could do this:
那么你可以这样做:
var value = $$('input[name=foo]:checked').getFirst().value;
Similarly, to get all checked checkboxes, use:
同样,要获取所有选中的复选框,请使用:
var values = $$('input[name=foo]:checked'??????????????).map(function(e) { return e.value; });
The double dollar ($$) function is used to select multiple elements with a CSS selector. Then a map (part of Array class) can be applied to those elements to get just the needed value(s).
双美元 ($$) 函数用于通过 CSS 选择器选择多个元素。然后可以将映射(Array 类的一部分)应用于这些元素以获取所需的值。
You can read more about these pseudo-selectors at http://mootools.net/docs/core/Utilities/Selectors
您可以在http://mootools.net/docs/core/Utilities/Selectors阅读有关这些伪选择器的更多信息
回答by adasdasdas
No need for
不需要
var value = $$('input[name=foo]:checked').getFirst().value;
Just use:
只需使用:
var value = $$('input[name=foo]:checked')[0].get('value');
回答by Dimcho
Easy way for getting checked radio's value is:
获得检查无线电价值的简单方法是:
$('formID').getElement('input[name=foo]:checked').get('value');
Gets the first descendant element whose tag name matches the tag provided. CSS selectors may also be passed.
获取标记名称与提供的标记匹配的第一个后代元素。也可以传递 CSS 选择器。
Mentioned earlier
之前提到
$$('input[name=foo]:checked').get('value');
is also easy way for getting all checked checkboxes' values, working with 1.4.5 verison
使用 1.4.5 版本获取所有选中复选框的值也是一种简单的方法
BR
BR
回答by vcxz
this works for me.
这对我有用。
$$('input[name=foo]:checked').pick().get('value');
回答by xavip
The best answer is from weiqianglufromchina, but has this error
最佳答案来自weiqianglufromchina,但有此错误
$$('input[name=type]:checked').get('value');
This should be
这应该是
$$('input[name=RADIONAME]:checked').get('value');
tested on firefox, chrome, explorer. mootools 1.2.4 / 1.3
在 Firefox、Chrome、Explorer 上测试。mootools 1.2.4 / 1.3
cheers
干杯
回答by weiqianglufromchina
Here's how to get the radio's value:
以下是获取收音机值的方法:
var value =$$('input[name=type]:checked').get('value');
Quite easy....
很简单....
回答by dawez
Just used this one lately in Mootools 1.11. I use:
最近刚刚在 Mootools 1.11 中使用了这个。我用:
var value = $$('input[name=foo]).filter(function(el) {
return el.getProperty("checked")
})[0];
Quite long but working. Tried with FF 3.x
很长但工作。试过 FF 3.x
More recent version of Mootools can use a shorter syntax as the other answers provided.
更新版本的 Mootools 可以使用更短的语法作为提供的其他答案。
回答by sleep-er
The above code worked fine in Firefox but IE didn't like it at all. After a bit of tweaking came up with this horrible work around.
上面的代码在 Firefox 中运行良好,但 IE 根本不喜欢它。经过一些调整,想出了这个可怕的工作。
Add onchange="toggleChecked(this)" to each of the radios and then add this function into you JS file
将 onchange="toggleChecked(this)" 添加到每个收音机,然后将此功能添加到您的 JS 文件中
function toggleChecked(el) {
el.checked = !el.checked;
el.checked = !el.checked;
}
and then to read which button has been selected
然后读取已选择的按钮
var selectFoo;
$$('input[name=foo]').each(function (el){
if (el.checked == true) {
selectFoo = el.value;
}
});
Now if somebody could come up with a better IE solution I would appreciate it.
现在,如果有人能提出更好的 IE 解决方案,我将不胜感激。
回答by serviom
nothing not work in Firefox 3.15.16 except expression:
除了表达式,在 Firefox 3.15.16 中没有什么不起作用:
var radio = $('adminForm').getElements("input[name='published']").filter(function(e) { if(e.checked) return e; })[0].value;
var radio = $('adminForm').getElements("input[name='published']").filter(function(e) { if(e.checked) return e; })[0].value;
Sorry for the English!!!
对不起英语!!!

