清除 IE 中的<选择>下拉列表

clear <select> dropdown in IE

本文关键字:选择 下拉列表 IE 中的 清除      更新时间:2023-09-26

我正在尝试清除带有子项的类型下拉列表,JQuery Empty()命令适用于Chrome(我假设所有其他浏览器),但不适用于Internet Explorer。

我试过这个

document.getElementById("dropdown").length = 0;

document.getElementById("dropdown").innerHtml("");

但返回

document.getElementById(...)' is null or not an object

.

   var DropDown = $("#dropdown");
    Dropdown.Length = 0; 

似乎没有任何效果(不确定是否可以在 JQuery 变量上使用纯 JavaScript 方法?

有什么可靠的方法来清除Internet Explorer中的元素吗?

HTML 很混乱,因为它是从 SharePoint 中的 ASP 生成的,但这是浏览器和 JavaScript 从中读取的相关输出

我正在使用Jquery 1.11.3(来自谷歌APIS)和IE浏览器11

<tr>
        <td nowrap="true" valign="top" width="190px" class="ms-formlabel"><h3 class="ms-standardheader">
        <nobr>Interaction Contact</nobr>
    </h3></td>
        <td valign="top" class="ms-formbody">
        <!-- FieldName="Interaction Contact"
             FieldInternalName="Interaction_x0020_Contact"
             FieldType="SPFieldLookup"
          -->
            <span dir="none"><select name="ctl00$m$g_d04d1ad3_87fc_4751_9c85_4974c40486ca$ctl00$ctl05$ctl07$ctl00$ctl00$ctl04$ctl00$Lookup" id="ctl00_m_g_d04d1ad3_87fc_4751_9c85_4974c40486ca_ctl00_ctl05_ctl07_ctl00_ctl00_ctl04_ctl00_Lookup" title="Interaction Contact">
                    <option selected="selected" value="0">(None)</option>
                    <option value="15788"> John doe , john-doe@mail.net</option>
                    <option value="15788"> John doe , john-doe@mail.net</option>                    
                <!-- lots of <option> elements that needs to be cleared -->
                </select><br></span>
        </td>
    </tr>

在jquery中,你可以跨浏览器清除它:

$("#dropdown").empty()

尝试:

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

要使用 jQuery 执行此操作:

$('#dropdown').empty();