javascript如何设置下拉框的显示值和实际值
1个回答
展开全部
下拉框select
每一个选项是option
显示值是<option>显示值</option>
实际值是value,<option value="实际值">显示值</otpion>
默认选中是selected
下面是一个简单的例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
let select = document.createElement("select");
let option1 = document.createElement("option");
option1.setAttribute("value","1");
option1.innerText = "张三";
select.appendChild(option1);
let option2 = document.createElement("option");
option2.setAttribute("value","2");
option2.innerText = "李四";
option2.setAttribute("selected","");
select.appendChild(option2);
document.body.appendChild(select);
</script>
</body>
</html>
效果:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询