最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to Populate a List Box (combo box) from a Multi-Dimensional Array? - Stack Overflow

programmeradmin0浏览0评论

I'm a student in JavaScript and we have an assignment due tonight. I've been working, and re-working, the code for bit.

Essentially, I'm struggling with populating a List Box (bo box) from a Multi-Dimensional Array.

I have a multi-dimensional array created and it works (I sent my results to an alert box).

var concertArray = [
        ["Billy Joel", "99", "equal.png"],
        ["Bryan Adams", "89", "higher.png"],
        ["Brian Adams", "25", "lower.png"]

        ];

But, I have no idea how to populate this array to a select list box (bo box).

Any help, much appreciated.

I'm a student in JavaScript and we have an assignment due tonight. I've been working, and re-working, the code for bit.

Essentially, I'm struggling with populating a List Box (bo box) from a Multi-Dimensional Array.

I have a multi-dimensional array created and it works (I sent my results to an alert box).

var concertArray = [
        ["Billy Joel", "99", "equal.png"],
        ["Bryan Adams", "89", "higher.png"],
        ["Brian Adams", "25", "lower.png"]

        ];

But, I have no idea how to populate this array to a select list box (bo box).

Any help, much appreciated.

Share edited Mar 3, 2013 at 11:39 Mat 207k41 gold badges402 silver badges418 bronze badges asked Apr 22, 2012 at 12:36 kentrenholmkentrenholm 3333 gold badges7 silver badges23 bronze badges 3
  • 1 Do you know how to iterate over the array? – Felix Kling Commented Apr 22, 2012 at 12:42
  • What, exactly, should the generated HTML be for this example? Have you tried using a for loop? – Matt Ball Commented Apr 22, 2012 at 12:42
  • to add the columns of Billy Joel, Bryan Adams, and Brian Adams to appear in the select box. I then want to use the value to show price and the image to appear. I think I can do the other things once I can populate the bo box. – kentrenholm Commented Apr 22, 2012 at 13:00
Add a ment  | 

2 Answers 2

Reset to default 2

Use this Code.

<html>

    <head>
     <script type="text/javascript">
        var concertArray = [
            ["Billy Joel", "99", "equal.png"],
            ["Bryan Adams", "89", "higher.png"],
            ["Brian Adams", "25", "lower.png"]
            ];

            function populate(){

                for(i=0;i<concertArray.length;i++){
                    var select = document.getElementById("test");
                    select.options[select.options.length] = new Option(concertArray[i][0], concertArray[i][1]);
                }

            }
        </script>
        </head>

    <body onload="populate();">
        <select id="test">
        </select>
    </body>     
</html>

this will help you.....

extract the data which you want to add in bo box from the array, then use jquery append to add them to the select tag.

<select id="test">
</select>

Iterate through the array & store the data to be added in a variable, say var data, then:

$('#test').append('<option value="'+data+'">'+data+'</option');
发布评论

评论列表(0)

  1. 暂无评论