【问题标题】:issue while creating a dynamic dropdown box using multiselect jquery plugin使用多选 jquery 插件创建动态下拉框时出现问题
【发布时间】:2019-11-19 23:56:41
【问题描述】:

我正在尝试创建一个动态选择下拉列表,我尝试在 javascript 和 jquery 中编写代码,但无法在下拉列表中附加选项。当我尝试查看存储的内容时,会显示 Html 选择代码以及所有选项。我应该在其中添加或更改什么以便将选项存储在正确的位置?

lines=["B","C","D","E","F","G"];
$(document).ready(function(){
  $("#select1").multiselect({
    placeholder: 'Features'
  });
});    

   // var selectFir = document.getElementById("select1");
      
   // for (var i = 0; i < firstLine.length; i++) {
         // var x = document.createElement("OPTION"), 
           //     txt = document.createTextNode(firstLine[i]);
          // x.appendChild(txt);
          //  x.setAttribute("value", firstLine[i]);
          //  selectFir.appendChild(x);
          //  selectFir.insertBefore(x, selectFir.lastChild);
   //  }console.log(selectFir);

firstLine = lines[0];
// Calls function to generate drop downs using options above
       for(var i=0; i< firstLine.length;i++)
       {
        //creates option tag
         jQuery('<option/>', {
               value: firstLine[i],
               html: firstLine[i]
               }).appendTo('#select1'); 
       }
       jQuery('#select1').multiselect('refresh');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/multiselect/2.2.9/js/multiselect.min.js"></script>
<select multiple id="select1">
 <option value="A">A</option>
</select>

【问题讨论】:

  • 看起来您期待在某些点击事件的函数中“//调用函数以使用上面的选项生成下拉菜单”之后放置代码,对吗?
  • 是的,问题是当我显示selectFir时我可以看到html代码的整个select标签,它没有推送options标签中的选项。是否有可以填充动态下拉列表的 jquery 插件?
  • 我提供的解决方案代码不是回答你的问题吗?
  • 哦,是的,这有帮助,抱歉我没有回复,谢谢!

标签: javascript jquery html bootstrap-multiselect


【解决方案1】:

lines=["B","C","D","E","F","G"];
$(document).ready(function(){
  $("#select1").multiselect({
    placeholder: 'Features'
  });
});    

   // var selectFir = document.getElementById("select1");
      
   // for (var i = 0; i < firstLine.length; i++) {
         // var x = document.createElement("OPTION"), 
           //     txt = document.createTextNode(firstLine[i]);
          // x.appendChild(txt);
          //  x.setAttribute("value", firstLine[i]);
          //  selectFir.appendChild(x);
          //  selectFir.insertBefore(x, selectFir.lastChild);
   //  }console.log(selectFir);

firstLine = lines;
// Calls function to generate drop downs using options above
       for(var i=0; i< firstLine .length;i++)
       {
        //creates option tag
         jQuery('<option/>', {
               value: firstLine[i],
               html: firstLine[i]
               }).appendTo('#select1'); 
       }
       jQuery('#select1').multiselect('refresh');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/multiselect/2.2.9/js/multiselect.min.js"></script>
<select multiple id="select1">
 <option value="A">A</option>
</select>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多