开发者

Remove then Re-Add option elements from SELECT menu?

开发者 https://www.devze.com 2023-02-19 16:07 出处:网络
Given the followi开发者_如何学Pythonng menu http://jsfiddle.net/pYJPc/ using Javascript, how would I iterate through all options and remove them one by one? then re-add them all. I don\'t want the sel

Given the followi开发者_如何学Pythonng menu http://jsfiddle.net/pYJPc/ using Javascript, how would I iterate through all options and remove them one by one? then re-add them all. I don't want the select menu itself to be removed at all


Here is one way to do it using Vanilla JavaScript JSFiddle Demo:

Here is the markup HTML:

<select id="myselect">
    <option value='none'>--Select a page--</option>
    <option value="1">W3Schools</option>
    <option value="2">Microsoft</option>
    <option value="3">AltaVista</option>
</select>
<br/><br/>
<button value='add' id='addbtn' name='addbtn'>add</button>
<button value='delete' id='deletebtn' name='deletebtn'>delete</button>

Using cloneNode to backup your default select options. The addOption will add the backup back to your select if there is no options and the deleteOption will delete all options in your select tag:

//clone our options to a backup
var myselect = document.getElementById('myselect');
var backup = myselect.cloneNode(true).getElementsByTagName('option');
//add backup back into select
function addOption() {
    if (myselect.options.length == 0) {
        for (var i = 0; i < backup.length; i++) {
            myselect.options.add(backup[i].cloneNode(true));
        }
    }
}
//delete all option in select
function deleteOption() {
    while (myselect.options.length != 0) {
        myselect.options.remove(myselect.options.length - 1);
    }
}
//attach click event to btns
document.getElementById('addbtn').onclick = addOption;
document.getElementById('deletebtn').onclick = deleteOption;

Turns out in IE cloneNode does not really clone it. So, we'll have to create our own cloneNode, and change the backup to:

var backup = IEcloneNode(myselect).getElementsByTagName('option');

//FOR IE 
//Reference http://brooknovak.wordpress.com/2009/08/23/ies-clonenode-doesnt-actually-clone/
function IEcloneNode(node) {
    // If the node is a text node, then re-create it rather than clone it
    var clone = node.nodeType == 3 ? document.createTextNode(node.nodeValue) : node.cloneNode(false);
    // Recurse
    var child = node.firstChild;
    while(child) {
        clone.appendChild(IEcloneNode(child));
        child = child.nextSibling;
    }

    return clone;
}


Your HTML :

<select id="menu" onchange="go()">
    <option>--Select a page--</option>
    <option value="1">W3Schools</option>
    <option value="2">Microsoft</option>
    <option value="3">AltaVista</option>
</select>
<input id="remove" type="button" value="remove one" >
<input id="repop" type="button" value="repop">

And your JS with jQuery

var buffer=new Array();
$("#remove").click(function() {
    buffer.push($("option:first").get(0));
    $("option:first").remove();
});
$("#repop").click(function() {
    $("select").append(buffer);
});


With jQuery loaded:

var options = $('#menu').children();
children.remove();
$('#menu').insert(children);

Similarly in different libraries as well.

Without a library, you need a little bit more work :)

0

精彩评论

暂无评论...
验证码 换一张
取 消