
我要做的是在选择特定选项时更改禁用输入字段的状态(类型为文本). HTML看起来有点像tis:
<select 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" />
这就是我HOPing会给我在if语句中检查的值,然后将输入禁用更改为启用:
window.addEvent('domready',function(){ $$('#selectBox').addEvent('change',function(){ var selection = $$('#selectBox').getSelected(); alert(selection); });}); 当我们运行代码(即我在选项框中选择另一个值)时,我得到的是[object HTMLOptionElement].
关于此方法的mootools的文档是SPARSE,只说:
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的
$('country-select').getSelected(); //Returns whatever the user selected. 注意:
无论select元素的多个属性如何,此方法都返回一个数组.如果select是单个,则它将返回仅包含一个项目的数组.
完全令人困惑!
有人请告诉我我错过了什么.我也尝试过:
var selection = $$('#selectBox').getSelected().value; //andvar selection = $$('#selectBox').getSelected('value'); //and//a WHolE bunch of other wild IDeas includingvar selection = $$('#selectBox').getSelected();alert(selection[0]); 什么都没有出来.在某些情况下,我得到未定义,在其他情况下,我得到相同的[对象HTMLOptionElement].
解决方法 这么多错了,不知道从哪里开始.$$()是一个集合运算符(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}); 确保你检查你的标记 – 你没有关闭选项,你有一个缺少“from name =”选项>同样.
getSelected作为方法存在,因为某些选择使用多个选择,因此执行selectEl.get(“value”)将不会报告任何有意义的内容.在任何其他情况下,.get(“value”)都没问题.
检查它是否有效:
http://www.jsfiddle.net/dimitar/SmShF/
玩得开心,阅读手册:)
总结以上是内存溢出为你收集整理的html – MooTools – 如何使用getSelected()全部内容,希望文章能够帮你解决html – MooTools – 如何使用getSelected()所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)