jQuery动态增加/删除select下来框的option中的selected属性
1、说明:选中下拉框的时候,先清除所有选中的option选项
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src='jquery的包路径'></script>
</head>
<body>
<select name="select" id="selID">
<option value="0">下拉菜单一</option>
<option value="1">下拉菜单二</option>
<option value="2" selected="selected">下拉菜单三</option>
<option value="3">下拉菜单四</option>
<option value="4">下拉菜单五</option>
</select>
<script type="text/javascript">
$(function () {
$("#selID").change(function () {
$("#selID").each(function () {
$(this).find("option").each(function () {
if ($(this).attr("selected")) {
$(this).removeAttr("selected");
}
});
});
$("#selID").find("option:selected").attr("selected", true);
})
alert($("#selID").find("option:selected").text())
})
</script>
</body>
</html>
2、效果截图:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/14aa5f84e66025128a2b1cec35af37e5.png)