'';选项“;来自这个JavaScript代码块?
第一名:>'';选项“;来自这个JavaScript代码块?,javascript,function,for-loop,html-select,Javascript,Function,For Loop,Html Select,第一名:> why it 'options' does work fine inside the function and not work outside the function “选项””在此块中来自何处:- for(变量i=0;i why it 'options' does work fine inside the function and not work outside the function 这是密码- <form name="selectForm">
why it 'options' does work fine inside the function and not work outside the function
“选项””在此块中来自何处:-
for(变量i=0;i
*
秒:>
why it 'options' does work fine inside the function and not work outside the function
这是密码-
<form name="selectForm">
<p>
<label for="musicTypes">Choose some music types, then click the button below:</label>
<select id="musicTypes" name="musicTypes" multiple="multiple">
<option selected="selected">R&B</option>
<option>Jazz</option>
<option>Blues</option>
<option>New Age</option>
<option>Classical</option>
<option>Opera</option>
</select>
</p>
<p><input id="btn" type="button" value="How many are selected?" /></p>
</form>
<script>
function howMany(selectObject) {
var numberSelected = 0;
for (var i = 0; i < selectObject.options.length; i++) {
if (selectObject.options[i].selected) {
numberSelected++;
}
}
return numberSelected;
}
var btn = document.getElementById('btn');
btn.addEventListener('click', function() {
alert('Number of options selected: ' + howMany(document.selectForm.musicTypes));
});
</script>
选择一些音乐类型,然后单击下面的按钮:
R&B
爵士乐
忧郁
新时代
古典的
歌剧院
函数数量(选择对象){
var numberSelected=0;
对于(变量i=0;i1)选项
是HTML DOM的一部分,负责在指定的选择
元素中选择选项
元素
2) 它确实在任何select元素的函数之外工作,但我假设您试图在函数之外使用selectObject
,这违反了变量范围
阅读更多:
[1]
[2] “本地javascript的一部分”-不,它不是。它是HTML DOM的一部分。对不起,你完全正确。看来我今天还没有醒来。更正:)非常感谢,祝您愉快,先生