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

javascript - Populating a drop down box with a variable containing comma seperated values - Stack Overflow

programmeradmin1浏览0评论

How can I, populate a select box based on a variable that will contain data by ma separated values?

Example:

var x = Jenny,John,Stephanie,Marc,Ryan,Jessica

Expected result:

[DROP DOWN BOX]
Jenny
John
Stephanie
Marc
Ryan
Jessica

How can I, populate a select box based on a variable that will contain data by ma separated values?

Example:

var x = Jenny,John,Stephanie,Marc,Ryan,Jessica

Expected result:

[DROP DOWN BOX]
Jenny
John
Stephanie
Marc
Ryan
Jessica
Share Improve this question asked Jan 25, 2013 at 14:37 John SmithJohn Smith 1,68711 gold badges38 silver badges52 bronze badges 3
  • Do you mean x is a string or an array with values? – Yoggi Commented Jan 25, 2013 at 14:39
  • This question is similar: stackoverflow./questions/4514323/… – Schleis Commented Jan 25, 2013 at 14:40
  • stackoverflow./questions/9895082/… check this – user1992977 Commented Jan 25, 2013 at 14:40
Add a ment  | 

5 Answers 5

Reset to default 3

http://jsfiddle/fYN7M/

HTML

<select id="myoptions">

</select>

JavaScript

var x = "Jenny,John,Stephanie,Marc,Ryan,Jessica";

var options = x.split(",");

var select = document.getElementById('myoptions');
for(var i=0; i<options.length; i++)
{
  select.options[i] = new Option(options[i], i);  //new Option("Text", "Value")
}

Something like this should work:

<select id="DropDownList">

</select>

<script type="text/javascript" language="javascript">
   var x = "Jenny,John,Stephanie,Marc,Ryan,Jessica";
   var splitValues = x.split(",");
   for (var i = 0; i < splitValues.length; i++) {
      var opt = document.createElement("option");

      // Add an Option object to Drop Down/List Box
      document.getElementById("DropDownList").options.add(opt);

      // Assign text and value to Option object
      opt.text = splitValues[i];
      opt.value = splitValues[i];
   }
</script>

Here's some pure javascript for you:

var x = 'Jenny,John,Stephanie,Marc,Ryan,Jessica';
x = x.split(',');

select = document.createElement('select');

for (var i=0;i<x.length;i++) { 
    var new_option_element = new Option(x[i], x[i]);
    select.appendChild(new_option_element);
}

document.body.appendChild(select);

http://jsfiddle/crowjonah/ZwtUF/1/

A simple means of this is:

function populateWithChildren(parent, childTag, values) {
  if (!parent || !values) {
    // no parent element, or no values, nothing can be done so quit here
    return false;
  } else {
    // this ensures that 'values' is an array:
        // if values is a string, it splits that string (assuming a ma delimiter),
        // and split() returns an array, otherwise:
        // we assume it's already an array and use that.
    values = typeof values === "string" ? values.split(',') : values;
    // iterates through all the values in the 'values' array,
    for (var i = 0, len = values.length; i < len; i++) {
      // creates a new element (as passed in the 'childTag' variable)
      var newElem = document.createElement(childTag),
          text = document.createTextNode(values[i]);
      // appends the textNode to the created element
      newElem.appendChild(text);
      // appends the created-element to the 'parent' node passed to the function
      parent.appendChild(newElem);
    }
  }
}

var parent = document.getElementById('select'),
  x = 'Jenny,John,Stephanie,Marc,Ryan,Jessica';

populateWithChildren(parent, 'option', x);

JS Fiddle demo.

JavaScript

var x = 'Jenny,John,Stephanie,Marc,Ryan,Jessica'.split(',');
for (var i=0; i<x.length; i++) {
    document.getElementById("names").options[i] = new Option(x[i], x[i]);
}

HTML

<select id="names"></select>

Demo

http://jsfiddle/hyA96/

发布评论

评论列表(0)

  1. 暂无评论