我正在使用 Web2Py 将值列表发送到我的视图。
我的 HTML 是这样的:
<select id="DestinationOptions" onchange="SetDest(this)">
{{for key, value in dirList.iteritems():}}
<option name="DirListItem{{=key}}" id="{{=key}}"> {{=value}}</option>
{{pass}}
</select>
我的 JavaScript 是这样的:
function SetDest(destComboBxID)
{
alert(destComboBxID.id);
var e = document.getElementById(destComboBxID);
var path = e.option[e.selectedIndex].value;
alert(path);
//document.cookie = "GD_"+file.id + "=" + file.id + ";";
}
我只得到第一个alert()
然后在浏览器中调试时出现以下错误:
Uncaught TypeError: Cannot read property 'options' of null
问:如何获取所选值的ID?
您可以将 EventListener 添加到选择字段。当它改变时,你可以通过使用获取所选选项的IDe.target.options[e.target.selectedIndex].getAttribute('id')
.
document.getElementById('DestinationOptions').addEventListener('change', function(e) {
console.log(e.target.options[e.target.selectedIndex].getAttribute('id'));
});
<select id="DestinationOptions">
<option id="1">Hello</option>
<option id="2">World</option>
</select>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)