Javascript - 获取dropbox菜单的属性

时间:2017-09-23 21:39:55

标签: javascript html drop-down-menu options

我有这个HTML代码:

<select id="ITAAServerList" name="ITAAServerList" style="visibility:visible">
     <option ServerName="SERVER01" IPAddress="192.168.46.1" StartPageName="SERVER01_Start.html">SERVER01</option>
     <option ServerName="SERVER02" IPAddress="192.168.46.2" StartPageName="SERVER02_Start.html">SERVER02</option>
     <option ServerName="SERVER03" IPAddress="192.168.46.3" StartPageName="SERVER03_Start.html">SERVER02</option>
</select>
<button type="submit" name="oNext" id="oNext" onclick ="OnClickNext()" class="item cursor-pointer login-button">LOGIN</button>
...
<script type="text/javascript" src="onclicknext.js"></script>

这个JS代码:

function OnClickNext() {
    var oITAAServer = document.getElementById('ITAAServerList').options[this.selectedIndex].getAttribute('StartPageName');  
    alert(oITAAServer);
};

我无法从下拉菜单中获取 StartPageName 属性。帮助

1 个答案:

答案 0 :(得分:0)

你几乎就在那里 - 你只需针对.selectedIndex元素而不是<select>this进行操作。

function OnClickNext() {
    var selectElement = document.getElementById('ITAAServerList');    
    var selectedOption = selectElement.options[ selectElement.selectedIndex ];  //not this.selectedIndex   
    var oITAAServer = selectedOption.getAttribute('StartPageName');  
    alert(oITAAServer);
};
<select id="ITAAServerList" name="ITAAServerList" style="visibility:visible">
     <option ServerName="SERVER01" IPAddress="192.168.46.1" StartPageName="SERVER01_Start.html">SERVER01</option>
     <option ServerName="SERVER02" IPAddress="192.168.46.2" StartPageName="SERVER02_Start.html">SERVER02</option>
     <option ServerName="SERVER03" IPAddress="192.168.46.3" StartPageName="SERVER03_Start.html">SERVER03</option>
</select>
<button type="submit" name="oNext" id="oNext" onclick ="OnClickNext()" class="item cursor-pointer login-button">LOGIN</button>