2013-07-29 56 views
0

如何使用java腳本設置選擇標記的默認選定索引?我的代碼運行良好,但我無法確定默認選定值(例如)第一個值。使用JavaScript設置選擇標記的選定索引

var CompanySelect = document.getElementById("ManagingCompaniesSelect"); 
if (result && result.length > 0) { 
    CompanySelect.options[CompanySelect.options.length] = new Option(_seletCompany, -1); 

    for (var i = 0; i <= result.length-1; i++) 
    { CompanySelect.options[CompanySelect.options.length] = new Option(result[i].ManagingCompanyName, result[i].MangingCompanyGUId); } 
} 




var txtManagingCompany = '#ManagingCompaniesSelect' + ' option[value=-1]'; 
$(txtManagingCompany).attr("selected", "selected"); 

回答

1

您可以使用selectedIndex屬性。

CompanySelect.selectedIndex = 0; 
// will select the first option by default and so on.. 
+0

你能告訴我你的代碼,它不工作,@克里斯 – Alaa

+0

? – mohkhan

+0

我已經添加了它@mohkan – Alaa

相關問題