Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将项目追加到组合框中_Javascript_Html_Combobox - Fatal编程技术网

Javascript 将项目追加到组合框中

Javascript 将项目追加到组合框中,javascript,html,combobox,Javascript,Html,Combobox,我需要在组合框中添加特定次数的项。这是我的代码 for(i=0;i<3;i++) { othercompaniesli.innerHTML= '<select onchange="document.location.href = this.options[this.selectedIndex].value;"><option VALUE="http://www.google.com">'+fStr1[0]+'</o

我需要在组合框中添加特定次数的项。这是我的代码

         for(i=0;i<3;i++)
    {
         othercompaniesli.innerHTML=  '<select onchange="document.location.href = this.options[this.selectedIndex].value;"><option VALUE="http://www.google.com">'+fStr1[0]+'</option>  </select>';
     }

对于(i=0;i尝试
其他公司li.innerHTML+=

由于您使用的是等于
=
,因此它将重新分配给相同的元素

使用

$('#othercompaniesli')。追加(''+fStr1[0]+'');
请注意,您的
选择
选项
元素正在重复,您需要相应地更改它。

var tmpStr='';
var select= $('mySelect');
var opt = new Option("OptionTitle", "123");

select.selectedIndex = InsertNewOption(opt, select[0]);

function InsertNewOption(opt, element)
{
    var len = element.options.length;
    element.options[optsLen] = opt;

    return len;
}

对于(i=0;i将选择标记置于循环之外


var selectTag = '<select onchange="document.location.href = this.options[this.selectedIndex].value;">';
for(i=0;i<3;i++) {
 selectTag +=  '<option VALUE="http://www.google.com">'+fStr1[0]+'</option>';  
}
selectTag +="</select>"
othercompaniesli.innerHTML = selectTag;

var selectTag='';

对于(i=0;i您所做的是在循环内部结束select标记,因此每个元素都有自己的select开始和结束标记。您只是使用较新的元素更新您的innerHTML,这就是它获取最后一个元素的原因

 var openingTag= '<select onchange="document.location.href = this.options[this.selectedIndex].value;">';

for(i=0;i<3;i++)
{
    openingTag+=  '<option VALUE="http://www.google.com">'+fStr1[0]+'</option>  ';
}

openingTag= '</select>';

othercompaniesli.innerHTML = openingTag;
var openingTag='';

对于(i=0;i此处
select
opt
以错误的顺序传递到
InsertNewOption
此外,select是一个jQuery对象,而不是一个元素,因此需要作为select[0]或select传递。get(0)哦,是的,当然。Thx!修复了它。

var selectTag = '<select onchange="document.location.href = this.options[this.selectedIndex].value;">';
for(i=0;i<3;i++) {
 selectTag +=  '<option VALUE="http://www.google.com">'+fStr1[0]+'</option>';  
}
selectTag +="</select>"
othercompaniesli.innerHTML = selectTag;
 var openingTag= '<select onchange="document.location.href = this.options[this.selectedIndex].value;">';

for(i=0;i<3;i++)
{
    openingTag+=  '<option VALUE="http://www.google.com">'+fStr1[0]+'</option>  ';
}

openingTag= '</select>';

othercompaniesli.innerHTML = openingTag;