动态选择的选项分隔值

问题描述 投票:0回答:1

我想从Framework7创建该选择项:

 <a href="#" class="item-link smart-select smart-select-init">
    <!-- select -->
    <select name="fruits">
      <option value="apple" selected>Apple</option> 
      <option value="pineapple">Pineapple</option>
      ...

因此,在我的代码中,我从数据库中获取值,并创建了元素。我特别关注seleted属性:

_db_getvalues.company(id, function (result) {
            if (result.rows.length > ) {

                var element = document.getElementById(mySmartselect);

                for (var i = 0; i < 10 ; i++) {
                    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);
                }
            }

        });

但不起作用;

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="mysmartselect">

   </select>
   <div class="item-content" >
   <div class="item-inner" >
    div class="item-title">Group</div>
    </div>
    </div>
    </a>

我做错了什么?我没有在控制台上遇到错误。

javascript jquery html html-framework-7
1个回答
0
投票

您的代码中有一些语法问题

1- div标签之一没有关闭</div>

2-var element = document.getElementById("mySmartselect"); // its correct one

无论如何,如果我们假设您返回的数据工作正常,那么语法修复后的代码将起作用:

     var element = document.getElementById("mySmartselect");

                for (var i = 0; i < 5; i++) {
                    var option = document.createElement("option");
                    option.setAttribute('selected', 'apple'+i);
                    option.value = 'apple'+i
                    option.text = 'apple'+i;
                    option.setAttribute('data-identificador', 'apple'+i);
                    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>
© www.soinside.com 2019 - 2024. All rights reserved.