Javascript 选项选择的价值是通过手工创建的
我想通过以下方式创建此选择: 但不起作用 option.setAttribute('selected',result.rows.item(i).name) 对我的选项列表没有影响 这是HTML部分Javascript 选项选择的价值是通过手工创建的,javascript,jquery,html,html-framework-7,Javascript,Jquery,Html,Html Framework 7,我想通过以下方式创建此选择: 但不起作用 option.setAttribute('selected',result.rows.item(i).name) 对我的选项列表没有影响 这是HTML部分 <a class="item-link smart-select smart-select-init" data-open-in="popover"> <select name="" id="mysmarts
<a class="item-link smart-select smart-select-init" data-open-in="popover">
<select name="" id="mysmartselect">
</select>
<div class="item-content" >
<div class="item-inner" >
div class="item-title">Group</div>
</div>
</div>
</a>
但这就是我得到的
我做错了什么?我在控制台上没有遇到错误。您的代码有一些语法问题
1-一个div
标记没有关闭
2-varelement=document.getElementById(“mySmartselect”);//类名必须介于“
无论如何,如果我们假设您返回的数据工作正常,那么语法修复后的代码将正常工作:
var element=document.getElementById(“mySmartselect”);
对于(变量i=0;i<5;i++){
var option=document.createElement(“选项”);
option.setAttribute('selected','apple'+i);
option.value='apple'+i
option.text='apple'+i;
option.setAttribute('data-identificator','apple'+i);
元素。添加(选项);
}
团体
有时写“result”,有时写“resultado”。是否正确?所选属性没有值。选项上的mear existance是选项被选中的原因。if(result.rows.length>){
@thingEvery我已经修好了,对不起,你能把结果
输出到控制台并包含在你的问题中吗?另外,请修好@Taplar指出的打字错误。
_db_getvalues.company(id, function (result) {
if (result.rows.length > 0) {
var element = document.getElementById(mySmartselect);
var option = document.createElement("option");
option.setAttribute('selected', result.rows.item(i).name);
option.value = result.rows.item(i).name;
option.text = result.rows.item(i).name;
option.setAttribute('data-identificador', result.rows.item(i).name);
element.add(option);
}
});
<a class="item-link smart-select smart-select-init" data-open-in="popover">
<select name="" id="mysmartselect">
</select>
<div class="item-content" >
<div class="item-inner" >
div class="item-title">Group</div>
</div>
</div>
</a>