【发布时间】:2013-02-06 16:42:00
【问题描述】:
我创建了一个函数,在单击时使用另一个选择框的值填充一个选择框,它对系统中的大多数选择框都工作得很好,但我现在正在努力解决这个问题,哈哈:
用于测试所选值并在发现时发出警报的功能:
function updateSelectBox(parent, child){
for(i = 0; i < document.getElementById(parent).length; i++){
if(document.getElementById(parent).options[i].selected){
alert(document.getElementById(parent).options[i].value);
}
}
这个当被选中时不会提醒:
<input type="hidden" name="data[Series][3][Profession][Profession]" value="" id="Professions3_">
<select name="data[Series][3][Profession][Profession][]" multiple="multiple" id="Professions3" onchange="updateSelectBox("Professions3", "Specialisms3");">
<option value="24">Scientist</option>
选择时会发出警报:
<input type="hidden" name="data[Series][4][Profession][Profession]" value="" id="Professions4_">
<select name="data[Series][4][Profession][Profession][]" multiple="multiple" id="Professions4" onchange="updateSelectBox("Professions4", "Specialisms4");">
<option value="24">Scientist</option>
这是来自不同选择框的完整 html 输出,也是 WORKING
<div class="input select"><label for="Zones">Zones</label><input type="hidden" name="data[Series][0][Zone][Zone]" value="" id="Zones_">
<select name="data[Series][0][Zone][Zone][]" multiple="multiple" id="Zones" onchange="updateSelectBox("Zones", "Countries");">
<option value="4">Europe</option>
</select>
</div>
【问题讨论】:
-
它们都不适合我。
onchange="updateSelectBox("Professions3", "Specialisms3");"看起来不正确(引号)。如果您修复函数中的引号和语法错误,它似乎可以正常工作:jsfiddle.net/uGjug(尽管代码可以改进很多)。 -
@RocketHazmat:
HTMLSelectElements有一个length属性:developer.mozilla.org/en-US/docs/DOM/HTMLSelectElement。 -
那么为什么这对系统中的所有其他选择框都有效?
-
@FelixKling:我.....我知道 :-)
标签: javascript html