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

javascript - Add select options using jquery and arrays - Stack Overflow

programmeradmin0浏览0评论

Well, I have the really super simple stuff in jsfiddle. I am experimenting to make sure I can do it right in jquery first before I have to dynamically generate the jquery in C#. Basically the jQuery is suppose to populate an html based on values in an array.

You can view the fiddle here /

This is what I have so far. But its not working.

var makes = new array();
makes[0] = 'HOONDA';
makes[1] = 'MEETSUBITCHE';
makes[2]=  'NEWSMOBILE';
makes[3]=  'FJORD';

for(var i=0; i<makes.length; i++)
 {   
     $('#2').append('<option value=1>'+makes[i]+'</option>');
 }

And in the HTML:

<select id="2" multiple="multiple">
    <option> 10</option>    
</select>

Well, I have the really super simple stuff in jsfiddle. I am experimenting to make sure I can do it right in jquery first before I have to dynamically generate the jquery in C#. Basically the jQuery is suppose to populate an html based on values in an array.

You can view the fiddle here http://jsfiddle/KwVty/

This is what I have so far. But its not working.

var makes = new array();
makes[0] = 'HOONDA';
makes[1] = 'MEETSUBITCHE';
makes[2]=  'NEWSMOBILE';
makes[3]=  'FJORD';

for(var i=0; i<makes.length; i++)
 {   
     $('#2').append('<option value=1>'+makes[i]+'</option>');
 }

And in the HTML:

<select id="2" multiple="multiple">
    <option> 10</option>    
</select>
Share Improve this question asked Jul 12, 2013 at 17:28 Alexander Ryan BaggettAlexander Ryan Baggett 2,3974 gold badges36 silver badges67 bronze badges 5
  • 1 The option value is 1 in all cases. Replace /1/"+i+"/ – Lee Meador Commented Jul 12, 2013 at 17:31
  • 2 Array has an uppercase A, not a lowercase a. – gen_Eric Commented Jul 12, 2013 at 17:32
  • 30 seconds too late you are :-P – Naftali Commented Jul 12, 2013 at 17:33
  • 2 Some advice, check your developer tools / console. You would've seen an error about array not being defined. – gen_Eric Commented Jul 12, 2013 at 17:33
  • see my answer here: stackoverflow./questions/9995017/… – Walter Stabosz Commented Jul 12, 2013 at 17:40
Add a ment  | 

2 Answers 2

Reset to default 8

There is no such thing as array natively in javascript.

I think you meant to use new Array() (note the uppercase A) or []

array(wrong) is declared as Array

 var makes = new Array();

Working Demo http://jsfiddle/KwVty/1/

发布评论

评论列表(0)

  1. 暂无评论