Change the options array of a select list

前端 未结 5 1303
青春惊慌失措
青春惊慌失措 2020-12-03 10:29

Is there a way to change the options array of an html select list using javascript or mootools?

I need to replace the entire options set with a new one. In my ajax

5条回答
  •  离开以前
    2020-12-03 10:53

    HTML

    
    
    
    
    
    

    Javascript

    
    
    $("button").click(function(){
        var oldSel = $('.element').get(0);
    
        while (oldSel.options.length > 0) {
            oldSel.remove(oldSel.options.length - 1);
        }
    
        var newSel = $('.newSel').get(0);
    
        for (i = 0; i < newSel.length; i++)
        {
            var opt = document.createElement('option');
    
            opt.text = newSel.options[i].text;
            opt.value = newSel.options[i].value;
    
            oldSel.add(opt, null);
        }
    })
    

    Demo

提交回复
热议问题