2017-09-23 81 views
0

的屬性我有這樣的HTML代碼:使用Javascript - 獲得的Dropbox菜單

<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從下拉菜單中的屬性。幫幫我?

+0

剛剛更新的JS代碼。 –

回答

0

你幾乎在那裏 - 你只需要對元素執行.selectedIndex而不是對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>

+0

驚喜 - 驚喜 - 它的作品!謝謝@Duncan! –

+0

沒問題 - 祝您的項目好運! –

+0

這樣做,但我建議避免很長的行,做很多事情 - 使代碼難以維護和管理。 –