使用JavaScript设置select标记的选定索引

使用JavaScript设置select标记的选定索引,javascript,Javascript,如何使用java脚本设置select标记的默认选定索引?我的代码运行良好,但无法确定默认的选定值(例如)第一个值 var CompanySelect = document.getElementById("ManagingCompaniesSelect"); if (result && result.length > 0) { CompanySelect.options[CompanySelect.options.length] = new Option(_sel

如何使用java脚本设置select标记的默认选定索引?我的代码运行良好,但无法确定默认的选定值(例如)第一个值

  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");
var CompanySelect=document.getElementById(“管理公司选择”);
if(result&&result.length>0){
CompanySelect.options[CompanySelect.options.length]=新选项(_seletCompany,-1);

对于(var i=0;i,您可以使用
selectedIndex
属性

示例

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

我已经把它加起来了@mohkanI是指您正在使用selectedIndex属性的那一个@mohkan您可以检查它now@Alaa:“它不工作”。在其上,这是一个无用的注释。更有用的是描述当您尝试提供的代码时会发生什么。