慕勒3428872
2018-10-18 12:14:40
使用$("select").html("")來清空一個動態生成的下拉框當下拉選項只有一個的時候,清空操作結束后,代碼清空了但是下拉框依舊顯示選擇項如圖所示再點擊一下下拉框選擇項才消失當有多個下拉選項的時候則不會出現這種情況寫了一個簡易的代碼<!DOCTYPE html><html><head> <title></title> <style> button { width: 50px; height: 20px; } </style> <script src="https://code.jquery.com/jquery-1.11.3.js"></script></head><body> <select></select> <button>按鈕</button> <script> var accountHtml = "", account = ["1234123421"]; account.forEach(function(i) { accountHtml += '<option value="' + i + '">' + i + '</option>'; }); $("select").html(accountHtml); $("button").on("click", function() { $("select").html(""); }); </script></body></html>
1 回答

汪汪一只貓
TA貢獻1898條經驗 獲得超8個贊
理論上不會出現~$("select").html("").val("");
試一下這個.
或者既然都清空了, 可以直接remove掉, 在生成一個也可以.
下面代碼我測試一下清空select沒問題~
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="jquery.min.js"></script>
</head>
<body>
<select name="" id="select"></select>
<button class="dianwo">dianwo</button>
<script>
$(function(){
$("select").append("<option value='123'>123</option><option value='456'>456</option><option value='789'>789</option>");
$(".dianwo").click(function(){
$("select").html("");
})
});
</script>
</body>
</html>
添加回答
舉報
0/150
提交
取消