访问 Mootools 数组的元素
我正在使用一些 Mootools 来访问 HTML select 元素的值,但问题是使用 Mootools [.getSelected()] 执行此操作的方法返回一个数组,我不知道如何处理它。
我的代码:
<script type="text/javascript">
window.addEvent('domready', function(){
$('votconj').addEvent('click', function() {
// This works great
$('jj_conjoint').addClass("validate['required']");
$('mm_conjoint').addClass("validate['required']");
$('aaaa_conjoint').addClass("validate['required']");
$('conjoint_regime').addClass("validate['required']");
new FormCheck('form');
});
if ($('nb_children').getSelected() == 1){
// this doesn't work because .getSelected() returns an array and never equals 1
$('jj_enfant1').addClass("validate['required']");
$('mm_enfant1').addClass("validate['required']");
$('aaaa_enfant1').addClass("validate['required']");
new FormCheck('form');
}
if ($('nb_children').getSelected() == 2){
// this doesn't work because .getSelected() returns an array and never equals 2
$('jj_enfant2').addClass("validate['required']");
$('mm_enfant2').addClass("validate['required']");
$('aaaa_enfant2').addClass("validate['required']");
new FormCheck('form');
}
new FormCheck('form');
});
</script>
I'm using a bit of Mootools to access the values of an HTML select element but the thing is that the way to do it with Mootools [.getSelected()] returns an array and I don't know how to handle it.
my code :
<script type="text/javascript">
window.addEvent('domready', function(){
$('votconj').addEvent('click', function() {
// This works great
$('jj_conjoint').addClass("validate['required']");
$('mm_conjoint').addClass("validate['required']");
$('aaaa_conjoint').addClass("validate['required']");
$('conjoint_regime').addClass("validate['required']");
new FormCheck('form');
});
if ($('nb_children').getSelected() == 1){
// this doesn't work because .getSelected() returns an array and never equals 1
$('jj_enfant1').addClass("validate['required']");
$('mm_enfant1').addClass("validate['required']");
$('aaaa_enfant1').addClass("validate['required']");
new FormCheck('form');
}
if ($('nb_children').getSelected() == 2){
// this doesn't work because .getSelected() returns an array and never equals 2
$('jj_enfant2').addClass("validate['required']");
$('mm_enfant2').addClass("validate['required']");
$('aaaa_enfant2').addClass("validate['required']");
new FormCheck('form');
}
new FormCheck('form');
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
getSelected()
返回一个数组,因为某些 select 元素允许多重选择。如果您的没有,您可以尝试$('nb_children').getSelected()[0]
。要获取值,您可以使用$('nb_children').getSelected()[0].get("value")
。getSelected()
returns an array because some select elements allow multiple selection. If yours doesn't, you could just try$('nb_children').getSelected()[0]
. To get the value you can use$('nb_children').getSelected()[0].get("value")
.您可以使用
.each
在 MooTools 中遍历数组:有关详细信息:http://mootools.net/docs/core/Types/Array#Array:each
getSelected()
始终返回数组的原因是代码像这样总是可以重复使用的时候您决定添加多个可选项目而不是仅添加一个。编辑
注意,上面的代码是直接写的。可能需要一些调整才能使其适合您的情况。
编辑 2
将代码更新为更全面的示例。
You can use
.each
to traverse an array in MooTools:For more info: http://mootools.net/docs/core/Types/Array#Array:each
The reason why
getSelected()
returns an array at all times, is that code like this can always be reused when you decide to add multiple selectable items instead of just one.Edit
Note that the above code is directly written. Might need some tweaking to get it working for your case.
Edit 2
Updated code to a more comprehensive example.
您想检查所选项目的值,对吗?
尝试使用:
You want to check the value of the selected item, right?
Try with: