这是因为它在for循环之后呈现代码,
所以在JS中,它是被选中的,而不是在HTML中。
要选择默认值,应执行内容数组的其他for循环,然后在其中选择该值。
document.getElementById("list").innerHTML = "";
var element={firstName: "mario",lastName: "rossi",email: "test@test.ts",role:{rolePriority:2}};
var element2={firstName: "mario2",lastName: "rossi",email: "test2@test.ts",role:{rolePriority:1}};
var element3={firstName: "mario3",lastName: "rossi",email: "test3@test.ts",role:{rolePriority:2}};
var content = [];
content.push(element);
content.push(element2);
content.push(element3);
var i;
var list = document.getElementById("list");
// Printing employees
for (i = 0; i < content.length; i++) {
list.innerHTML += content[i].firstName +
" " +
content[i].lastName +
" " +
content[i].email +
" " +
content[i].role.rolePriority +
" ";
var select = document.createElement("select");
var option1 = document.createElement("option");
option1.value = "0";
option1.text = "Superadmin";
var option2 = document.createElement("option");
option2.value = "1";
option2.text = "Admin";
var option3 = document.createElement("option");
option3.value = "2";
option3.text = "User";
select.appendChild(option1);
select.appendChild(option2);
select.appendChild(option3);
select.id = "select_" + content[i].email;
list.appendChild(select);
list.innerHTML += '<a href="#" onClick="updateRole(\'' + content[i].email + '\')">update role</a> </br>';
}
for (i = 0; i < content.length; i++) {
document.getElementById("select_" + content[i].email).value=content[i].role.rolePriority;
}
<div id="list">
</div>