如何使用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");
答案 0 :(得分:1)
您可以使用selectedIndex
属性。
示例强>
CompanySelect.selectedIndex = 0;
// will select the first option by default and so on..