Как я могу динамически удалить все параметры раскрывающегося списка в javascript?
Удаление всей опции выпадающего окна в javascript
Ответ 1
document.getElementById('id').options.length = 0;
или
document.getElementById('id').innerHTML = "";
Ответ 2
var select = document.getElementById('yourSelectBox');
while (select.firstChild) {
select.removeChild(select.firstChild);
}
Ответ 3
<select id="thing"><option>fdsjl</option></select>
<script>
var el = document.getElementById('thing');
el.innerHTML = '';
// or this
while ( el.firstChild ) {
el.removeChild( el.firstChild )
}
</script>
Ответ 4
Его очень легко использовать JavaScript и DOM:
while (selectBox.firstChild)
selectBox.removeChild(selectBox.firstChild);
Ответ 5
Настройка length
на 0, вероятно, является лучшим способом, но вы также можете сделать это:
var mySelect = document.getElementById("select");
var len = mySelect.length;
for (var i = 0; i < len; i++) {
mySelect.remove(0);
}
Ответ 6
Самое быстрое решение, которое я смог найти, - это следующий код (взятый из этого статья):
// Fast javascript function to clear all the options in an HTML select element
// Provide the id of the select element
// References to the old <select> object will become invalidated!
// This function returns a reference to the new select object.
function ClearOptionsFast(id)
{
var selectObj = document.getElementById(id);
var selectParentNode = selectObj.parentNode;
var newSelectObj = selectObj.cloneNode(false); // Make a shallow copy
selectParentNode.replaceChild(newSelectObj, selectObj);
return newSelectObj;
}