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;