2016-02-10 91 views
0
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(); 
    } 

我需要使選項添加並刪除我的選擇菜單中的某些選項。 其添加非常簡單,但不能刪除。刪除節點從選擇/組合javascript

當我嘗試從select/combo中刪除某個節點(如示例中)時,它總是刪除第一個節點。 如何刪除currentRSS

+0

您有什麼錯誤?你研究過這個問題嗎?請告訴我們你是如何試圖實現這一點的。請閱讀[如何問](http://stackoverflow.com/help/how-to-ask) –

回答

0

做的easyest方法是使用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]); 
    } 
}