我已經完成了這一百萬次,每一次都有效,但這次我無法使它工作,我不知道爲什麼。通過javascript獲取下拉列表的值不起作用。
我有一個下拉列表,我需要在javascript中獲取下拉列表的選定值。
<asp:DropDownList runat="server" ID="ddlCompanies" AutoPostBack="true">
<asp:ListItem Value="Microsoft Corporation, One Microsoft Way, Redmond, WA 98052-7329,USA">Microsoft Corporation</asp:ListItem>
<asp:ListItem Value="1600 Amphitheatre Parkway, Mountain View, CA 94043">Google Inc.</asp:ListItem>
<asp:ListItem Value="500 Oracle Parkway,Redwood Shores, CA 94065">Oracle Corporation</asp:ListItem>
<asp:ListItem Value="One Dell Way,Round Rock, Texas 78682,United States">Dell Inc.</asp:ListItem>
</asp:DropDownList>
我的JavaScript代碼是:
function findAddress() {
if (document.getElementById("ddlCompanies") == null)
return false;
else{
var ddlAddress = document.getElementById("ddlCompanies");
var value = ddlAddress.getAttribute('value');
var dllAddressIndex = ddlAddress.selectedIndex;
var dllAddressValue = dllAddressIndex.value;
var options = dllAddress.options[0];
var address = ddlAddress.options[ddlAddress.selectedIndex].value; // get selected address from dropdownlist
showAddress(address);
return false; // avoid postback
}
}
我運行這段代碼在調試模式下的螢火,我可以清楚地看到,所選擇的指數的值是零。實際上,代碼在達到dllAddress.options時會中斷。我也嘗試.text而不是.value,它仍然不起作用。請幫助我花了2天時間。謝謝。
他們不讓我發佈螢火蟲調試器的圖像,但我可以看到選定的索引是由javascript拾取的,但值仍然爲空。
謝謝。我真的很尷尬! – newcoder