繁体   English   中英

无法在Mozilla Firefox中使用javascript从下拉列表中删除项目

[英]Unable to remove an item from dropdown list using javascript in Mozilla Firefox

我是javascript的初学者。 我试图基于选项按钮从下拉列表中删除一些项目。 在firefox中,我看到它已到达删除项目的行,但没有删除项目。 你能帮忙吗?

 disable_dropdown_items() { var yes = document.getElementById('RadioYes').checked; var all_opts = document.getElementById('ALL_ITEMS').options; for (var i = 0; i < document.getElementById('ALL_ITEMS').options.length; i++) { if((document.getElementById('ALL_ITEMS').options[i].value == '891') && (yes == true)) { document.getElementById('ALL_ITEMS').options[i].remove(i); } } } 

线,

 document.getElementById('ALL_ITEMS').options[i].remove(i); 

在IE和chrome中可以很好地工作,并删除了值“ 891”,但是firefox不会删除。 我在这里想念什么吗? 我努力了:

document.getElementById('ALL_ITEMS').options.remove(i);

没有期权指数,仍然没有运气。

一个小得多的版本就是这样做。 在select上使用remove()

function disable_dropdown_items() {
    var yes = document.getElementById('RadioYes').checked;
    var sel = document.getElementById('ALL_ITEMS');

    for (var i = 0; i < sel.options.length; i++) {
        if ((sel.options[i].value == '891') && (yes == true)) {
            sel.remove(i); //Remove from select using index
        }
    }
}

一个小提琴的例子。

这删除了第三个选项,即拒绝

<select id="FilterByTypeTop" class="form-control">
    <option value="1">Approved</option>
    <option value="2">Not Approved</option>
    <option value="3">Rejected</option>
</select>
<script>
    $('#FilterByTypeTop').find('option[value="3"]').remove();
</script>

暂无
暂无

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

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