如何清除下拉框中的所有选项? [英] How do I clear all options in a dropdown box?

查看:277
本文介绍了如何清除下拉框中的所有选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在IE中运行,但在Safari,Firefox和Opera中断。 (大惊喜)

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

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

搜索后,我了解到它是 length = 0 它不喜欢。

我试过 ... options = null var清晰= 0; ... length = clear 具有相同的结果。

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.

我一次对多个对象这样做,所以我正在寻找一些轻量级JS代码。

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

推荐答案

您可以使用以下内容清除所有元素。请注意

You can use the following to clear all the elements. Note that

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

这篇关于如何清除下拉框中的所有选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆