从选择/组合 JavaScript 中删除节点

Delete node from select/combo javascript

本文关键字:删除 节点 JavaScript 组合 选择      更新时间:2023-09-26
RSS Channels
Add RSS to List: <input type="text" name="txtCombo" id="txtCombo"/>
<input type="button" value="Add " onclick="addCombo()">
<input type="button" value="Delete " onclick="delCombo()">
<br/>
Existing Channels : <select name="combo" id="combo"></select>

    var select = document.createElement("select");
    var combo = document.getElementById("combo");
    var option = document.createElement("option");
    option.text = "http://feeds.nytimes.com/nyt/rss/HomePage";
    option.value = "http://feeds.nytimes.com/nyt/rss/HomePage";
    currentRSS="http://feeds.nytimes.com/nyt/rss/HomePage"
    combo.add(option, null);

   function delCombo() {
        var combo = document.getElementById("combo");
        alert("Current Channel " + currentRSS);
        combo.remove(currentRSS);
        alert("Delete Channel " + currentRSS);
        numberRSSChannels--;
        document.getElementById("stat1").textContent="Numbers of RSS Channels " + numberRSSChannels;
        document.getElementById("combo").value="";
        currentRSS="";
        changeRSS();
    }

我需要 make 选项来添加和删除我的选择菜单中的某些选项。添加非常简单,但删除却不简单。

当我尝试从选择/组合中删除某个节点时,例如它总是删除第一个节点。如何删除currentRSS

最简单的方法是使用 jQuery:

$('#combo option[value="' + currentValue '"]').remove();

您需要在页面中添加对 jquery 的引用。作为替代方法,您可以循环访问组合的选项:

var combo = document.getElementById("combo");
var options = combo.options;
for (var index = 0; index < options.length; index ++) {
  if (options[index].value == currentValue) {
    combo.removeChild(options[index]);
  }
}