Javascript 使用包含逗号分隔值的变量填充下拉框
如何根据包含逗号分隔值数据的变量填充选择框 例如:Javascript 使用包含逗号分隔值的变量填充下拉框,javascript,Javascript,如何根据包含逗号分隔值数据的变量填充选择框 例如: var x = Jenny,John,Stephanie,Marc,Ryan,Jessica 预期结果: [DROP DOWN BOX] Jenny John Stephanie Marc Ryan Jessica JavaScript var x = 'Jenny,John,Stephanie,Marc,Ryan,Jessica'.split(','); for (var i=0; i<x.length; i++) { do
var x = Jenny,John,Stephanie,Marc,Ryan,Jessica
预期结果:
[DROP DOWN BOX]
Jenny
John
Stephanie
Marc
Ryan
Jessica
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]);
}
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")
}
var x='Jenny,John,Stephanie,Marc,Ryan,Jessica'.split(',');
对于(var i=0;iJavaScript
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]);
}
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")
}
var x='Jenny,John,Stephanie,Marc,Ryan,Jessica'.split(',');
对于(var i=0;i这样的东西应该可以工作:
<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>
var x=“珍妮、约翰、斯蒂芬妮、马克、瑞安、杰西卡”;
var splitValues=x.split(“,”);
对于(变量i=0;i
类似的方法应该可以:
<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>
var x=“珍妮、约翰、斯蒂芬妮、马克、瑞安、杰西卡”;
var splitValues=x.split(“,”);
对于(变量i=0;i
以下是一些纯javascript:
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);
var x='Jenny,John,Stephanie,Marc,Ryan,Jessica';
x=x.split(',');
select=document.createElement('select');
对于(var i=0;i,这里有一些纯javascript供您使用:
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);
var x='Jenny,John,Stephanie,Marc,Ryan,Jessica';
x=x.split(',');
select=document.createElement('select');
对于(var i=0;i而言,一个简单的方法是:
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 comma 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);
用子项(父项、子标记、值)填充函数{
如果(!parent | |!值){
//没有父元素,或没有值,无法执行任何操作,请退出此处
返回false;
}否则{
//这确保了“值”是一个数组:
//如果值是一个字符串,它将拆分该字符串(假定使用逗号分隔符),
//和split()返回一个数组,否则:
//我们假设它已经是一个数组并使用它。
values=typeof values===“字符串”?值。拆分(“,”):值;
//遍历“values”数组中的所有值,
对于(变量i=0,len=values.length;i
.一个简单的方法是:
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 comma 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);
用子项(父项、子标记、值)填充函数{
如果(!parent | |!值){
//没有父元素,或没有值,无法执行任何操作,请退出此处
返回false;
}否则{
//这确保了“值”是一个数组:
//如果值是一个字符串,它将拆分该字符串(假定使用逗号分隔符),
//和split()返回一个数组,否则:
//我们假设它已经是一个数组并使用它。
values=typeof values===“字符串”?值。拆分(“,”):值;
//遍历“values”数组中的所有值,
对于(变量i=0,len=values.length;i
.
HTML
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]);
}
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")
}
var x=“珍妮、约翰、斯蒂芬妮、马克、瑞安、杰西卡”;
var选项=x.split(“,”);
var select=document.getElementById('myoptions');
对于(var i=0;i
HTML
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]);
}
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")
}
var x=“珍妮、约翰、斯蒂芬妮、马克、瑞安、杰西卡”;
var选项=x.split(“,”);
var select=document.getElementById('myoptions');
对于(var i=0;i你是说x是一个字符串还是一个有值的数组?这个问题类似:检查这个你是说x是一个字符串还是一个有值的数组?这个问题类似:检查这个我相信IE在处理select
tag时设置innerHTML有问题我相信IE在处理sele时设置innerHTML有问题ct
tagsI不确定这与我2分钟前发布的答案有何不同,但哦,好吧。我不确定这与我7分钟前发布的答案有何不同,但哦,好吧。现在检查一下你的答案,区别在于小提琴,实际上不同。我使用了选项(“文本”、“值”)你使用了CreateElement.:)我不确定这与我2分钟前发布的答案有什么不同,但我不确定这与我7分钟前发布的答案有什么不同,但我不确定这与我7分钟前发布的答案有什么不同,但我现在检查一下你的答案,不同的是小提琴,它实际上是不同的。我使用了选项(“文本”,“值”)您使用了CreateElement.:)