简体   繁体   English

单击按钮清除填充的下拉列表

[英]Onclick of a button clear the populated drop-down list

I have a drop-down list which gets populated dynamically . 我有一个下拉列表,它是动态填充的。 I have a button which has to clear the drop-down list whenever the user wishes to. 我有一个按钮,它必须在用户希望时清除下拉列表。

test.html 的test.html

<select id="test">
<option></option>
</select>
<input type="button" id="refresh" />

here my select tag is populated dynamically. 在这里,我的选择标签是动态填充的。 Once I click on the refresh button the populated values should be cleared. 单击刷新按钮后,应清除填充的值。 How do i proceed? 我该如何进行?

Use the following code. 使用以下代码。

$('#refresh').click(function(e)
{
    $('#test').empty();
});

Uee following Uee关注

$('#refresh').click(function()
{
  $("#test").html("");
});

Means Expty HTML inside dropdown on click. 表示点击时在Expty HTML内部下拉菜单。

Check here Șhȇkhaṝ's answer .html("") is much faster than empty() . 在这里检查Șhȇkhaṝ的答案.html(“”)比empty()快得多。 here 这里

这将清除下拉值:

document.getElementById(‘drpFruits’).options.length=0;

Try the following: 请尝试以下操作:

document.getElementById('refresh').addEventListener('click', function () {
    document.getElementById('test').innerHTML = '';
});

No jQuery, clean native javascript code. 没有jQuery,干净的原生javascript代码。

Working fiddle . 工作提琴

var refreshBtn = document.getElementById("refresh");     
refreshBtn.addEventListener('click', function(event) {
document.getElementById("test").options.length = 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM