tra*_*ows 3 html forms mootools html-select
我正在尝试学习MooTools并且我是一个完整的javascript noobie所以请温柔地对待我.
我要做的是在选择特定选项时更改禁用输入字段的状态(类型为文本).html看起来有点像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" />
Run Code Online (Sandbox Code Playgroud)
这就是我HOPING会给我在if语句中检查的值,然后将输入禁用更改为启用:
window.addEvent('domready', function(){
$$('#selectBox').addEvent('change',function(){
var selection = $$('#selectBox').getSelected();
alert(selection);
});
});
Run Code Online (Sandbox Code Playgroud)
当我们运行的代码(即我在选项框中选择另一个值)时,我得到的是[object HTMLOptionElement].
关于此方法的mootools的文档是SPARSE,只说:
元素方法:getSelected
返回select元素的选定选项.
Returns:
* (array) An array of the selected elements.
Run Code Online (Sandbox Code Playgroud)
示例:HTML
<select id="country-select" name="country">
<option value="US">United States</option
<option value ="IT">Italy</option>
</select>
Run Code Online (Sandbox Code Playgroud)
JavaScript的
$('country-select').getSelected(); //Returns whatever the user selected.
Run Code Online (Sandbox Code Playgroud)
注意:
无论select元素的多个属性如何,此方法都返回一个数组.如果select是单个,它将返回一个只有一个项目的数组.
完全令人困惑!
有人请告诉我我错过了什么.我也尝试过:
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]);
Run Code Online (Sandbox Code Playgroud)
什么都没有出来.在某些情况下,我得到了undefined,在其他情况下,我得到相同[object HTMLOptionElement].
Dim*_*off 15
这么多错了,不知道从哪里开始.
$$()是一个集合运算符(document.getElements()根据选择器返回多个的别名) - 不适合用于id.
你想要document.id("idhere")或$("idhere")
对于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
});
Run Code Online (Sandbox Code Playgroud)
确保你检查你的标记 - 你没有关闭选项,你也缺少"from name ="选项>.
getSelected作为方法存在,因为某些选择使用多个选择,因此执行selectEl.get("value")将不会报告任何有意义的内容.在任何其他情况下,.get("value")都没问题.
检查它是否正常工作:http: //www.jsfiddle.net/dimitar/SmShF/
玩得开心,阅读手册:)