How do I clear all options in a dropdown box?

2020-01-24 06:44发布

My code works in IE but breaks in Safari, Firefox, and Opera. (big surprise)

document.getElementById("DropList").options.length=0;

After searching, I've learned that it's the length=0 that it doesn't like.
I've tried ...options=null and var clear=0; ...length=clear with the same result.

I am doing this to multiple objects at a time, so I am looking for some lightweight JS code.

27条回答
迷人小祖宗
2楼-- · 2020-01-24 07:02

Probably, not the cleanest solution, but it is definitely simpler than removing one-by-one:

document.getElementById("DropList").innerHTML = "";
查看更多
看我几分像从前
3楼-- · 2020-01-24 07:02

If you have to support IE and you have more than 100 items in your select list, I strongly recommend you consider replacing the select with a function like so:

function clearOptions(select) {
    var selectParentNode = select.parentNode;
    var newSelect = select.cloneNode(false); // Make a shallow copy
    selectParentNode.replaceChild(newSelect, select);
    return newSelect;
}

The select parameter should be the element either from a jquery selector or document.getElementBy call. The only downside to this is that you lose events you had wired up to the select, but you can easily reattach them as it is returned back out of the function. I was working with a select that had ~3k items and it would take 4 seconds on IE9 to clear the select so I could update it with the new content. Nearly instant doing it this way.

查看更多
在下西门庆
4楼-- · 2020-01-24 07:03

Today I was facing same problem, I did as below while reloading select box. (In Plain JS)

        var select = document.getElementById("item");
        select.options.length = 0;
        var opt = document.createElement('option');
        opt.value = 0;
        opt.innerHTML = "Select Item ...";
        opt.selected = "selected";
        select.appendChild(opt);


       for (var key in lands) {
            var opt = document.createElement('option');
            opt.value = lands[key].id;
            opt.innerHTML = lands[key].surveyNo;
            select.appendChild(opt);

        }
查看更多
霸刀☆藐视天下
5楼-- · 2020-01-24 07:06
var select = document.getElementById("DropList");
var length = select.options.length;
for (i = 0; i < length; i++) {
  select.options[i].remove();
}

Hope, this code will helps you

查看更多
男人必须洒脱
6楼-- · 2020-01-24 07:06

The simplest solutions are the best, so You just need:

var list = document.getElementById('list');
while (list.firstChild) {
    list.removeChild(list.firstChild);
}
<select id="list">
  <option value="0">0</option>
  <option value="1">1</option>
</select>

查看更多
叼着烟拽天下
7楼-- · 2020-01-24 07:07

Above answer's code need a slight change to remove the list complete, please check this piece of code.

var select = document.getElementById("DropList");
var length = select.options.length;
for (i = 0; i < length;) {
  select.options[i] = null;
  length = select.options.length;
}

refresh the length and it will remove all the data from drop down list. Hope this will help someone.

查看更多
登录 后发表回答