MooTools-如何使用getSelected() [英] MooTools - How to use getSelected()
问题描述
我正在尝试学习MooTools,并且是一个完全的javascript noobie,所以请对我保持温柔.
I'm trying to learn MooTools and am a TOTAL javascript noobie so please be gentle with me.
我想做的是选择一个特定选项时,更改一个禁用的输入字段(类型是文本)的状态. html看起来有点像tis:
What I'm tying to do is to change the state of a disabled input field (type is text) when a particular option is selected. The html looks a bit like tis:
<select class="wide" id="selectBox" name="option>
<optgroup label="Common">
<option value="one">One<option>
<option value="two">Two<option>
<option value="three">Three<option>
</optgroup>
<optgroup label="Less Common">
<option value="other">Other<option>
</optgroup>
</select>
<input id="other" type="text" disabled="disabled" />
这就是我希望在if语句中为我提供要检查的值,然后将输入更改为Disabled的功能:
This is what I was HOPING would give me the value to be checked in an if statement that would then change the input disabled to enabled:
window.addEvent('domready', function(){
$$('#selectBox').addEvent('change',function(){
var selection = $$('#selectBox').getSelected();
alert(selection);
});
});
当我们运行代码时(即我在选项框中选择另一个值),我得到的只是[object HTMLOptionElement]
.
When the code us run (i.e. I select another value in the option box) all I get is [object HTMLOptionElement]
.
有关该方法的mootools文档为SPARSE,仅说:
The documentation on mootools for this method is SPARSE and only says:
元素方法:getSelected
Element Method: getSelected
返回选择的选项 选择元素.
Returns the selected options of a select element.
Returns:
* (array) An array of the selected elements.
示例: HTML
<select id="country-select" name="country">
<option value="US">United States</option
<option value ="IT">Italy</option>
</select>
JavaScript
JavaScript
$('country-select').getSelected(); //Returns whatever the user selected.
注意:
此方法返回一个数组,而不管select元素的multi属性如何.如果选择为单个,它将返回仅包含一项的数组.
This method returns an array, regardless of the multiple attribute of the select element. If the select is single, it will return an array with only one item.
完全混乱!
有人请告诉我我想念的是什么.我也尝试过:
Someone please tell me what I'm missing. I've also tried:
var selection = $$('#selectBox').getSelected().value; //and
var selection = $$('#selectBox').getSelected('value'); //and
//a WHOLE bunch of other wild ideas including
var selection = $$('#selectBox').getSelected();
alert(selection[0]);
一切正常.在某些情况下,我会得到undefined
,在其他情况下,我会得到相同的[object HTMLOptionElement]
.
Nothing comes out properly. In some cases I get undefined
and in other cases I get the same [object HTMLOptionElement]
.
推荐答案
有很多错误,不确定从哪里开始.
so many things wrong, not sure where to begin.
$$()
是集合运算符(document.getElements()
的别名,它根据选择器返回倍数)-不适合用于ID.
$$()
is a collection operator (alias for document.getElements()
which returns multiples based upon a selector) - not appropriate to use for an id.
您想要document.id("idhere")
或$("idhere")
对于mootools 1.2 +
for mootools 1.2+
document.id('selectBox').addEvent('change',function() {
alert(this.get("value")); // get value
alert(this.getSelected().get("value")); // gets the option that's selected and then it's value
});
请确保您检查了标记-您没有关闭选项,而且name ="option>中也缺少".
make sure you check your markup - you don't close the options, you have a missing " from name="option> as well.
getSelected是一种方法,因为某些选择使用多项选择,因此执行selectEl.get("value")不会报告任何有意义的事情.在任何其他情况下,.get("value")都可以.
getSelected is there as a method as some selects use multiple selection so doing selectEl.get("value") will not report anything meaningful. any other case, .get("value") is fine.
检查它是否正常工作: http://www.jsfiddle.net/dimitar/SmShF/
check it working: http://www.jsfiddle.net/dimitar/SmShF/
玩得开心,请阅读手册:)
have fun and read the manual :)
这篇关于MooTools-如何使用getSelected()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!