找到你要的答案

Q:how to display selected item by using array of ids in javascript

Q:如何利用阵列IDS的JavaScript显示选定的项目

I have a small doubt i.e I have an array of ids (this array i got when selecting multiple items). From that array shall i display the selected items, but my array is only with ids. how can I do this please help me.

array was like this [001,003,005,002]

I have a small doubt i.e I have an array of ids (this array i got when selecting multiple items). From that array shall i display the selected items, but my array is only with ids. how can I do this please help me.

数组是这样[ 001003005002 ]

answer1: 回答1:

I think you might be looking for something like this:

showSelected = function(selArray) {
    var mySelect = document.getElementById('mySelect');
    var html="<ul>";
    for(i=0;i<selArray.length;i++) {
        html+="<li>" + mySelect[selArray[i]].text + "</li>";
    }
    html+="<ul>";
    document.getElementById('output').innerHTML = html;    
}

the key answer to your question is to iterate through your array of ids:

for(i=0;i<selArray.length;i++) {
    //....do smth
}

in there you can get the text out of the selected option like this

// assuming you have defined mySelect before!
mySelect[selArray[i]].text

working fiddle can be found here

我想你可能在找这样的东西:

showSelected = function(selArray) {
    var mySelect = document.getElementById('mySelect');
    var html="<ul>";
    for(i=0;i<selArray.length;i++) {
        html+="<li>" + mySelect[selArray[i]].text + "</li>";
    }
    html+="<ul>";
    document.getElementById('output').innerHTML = html;    
}

你的问题关键在于遍历数组的IDS:

for(i=0;i<selArray.length;i++) {
    //....do smth
}

在那里,您可以得到文本的选择选项,如

// assuming you have defined mySelect before!
mySelect[selArray[i]].text

工作小提琴可以在这里找到

javascript