简体   繁体   English

添加到 select 元素的动态选项无法正确呈现

[英]Dynamical options adding to select element not rendering correctly

I have read all the similar questions provided by SO, but no one is solving my issue.我已阅读 SO 提供的所有类似问题,但没有人解决我的问题。

I have the following html:我有以下 html:

<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">



<form action="" id="myForm">
   <div class="input-field col s12 l6">
       <select id="myselectid" name="myselectname">
       </select>
   </div>
   <div class="col s12 l6">
     <button id="" class="btn blue" type="submit"> submit</button>
   </div>
</form>

I'd need to populate the select element with values of the key "name" of a JSON file.我需要使用 JSON 文件的键“名称”的值填充 select 元素。

myJSON我的JSON

"resources": [
    {
      "name": "test.txt",
      "format": "txt",
      "size": 92502
    },
    {
      "name": "mini-test.txt",
      "format": "txt",
      "size": 64855
   }
]

myJS我的

<script>
 document.addEventListener('DOMContentLoaded', function() {
   $.getJSON(myJSON, function(data){
      var select = document.getElementById("myselectid");
      $.each(data.resources, function(i, field){
        select.add(new Option(field.name, field.name));
      });
    });
  });
  $(document).ready(function(){
     $('.sidenav').sidenav();
     $('select').formSelect();
  });
 </script>

I get a drop down menu with empty default value.我得到一个默认值为空的下拉菜单。 By clicking the arrow to expand the menu I get only the option "test.txt" .通过单击箭头展开菜单,我只得到选项"test.txt"

The odd thing is that the Firefox inspector shows the expected html in grey color奇怪的是 Firefox 检查器以灰色显示预期的 html

<select id="myselectid" name="myselectname" tabindex=-1>
  <option value="test.txt">test.txt</option>
  <option value="mini-test.txt">mini-test.txt</option>
</select>

What is going on?到底是怎么回事?

Any help is really welcome..任何帮助都非常受欢迎..

Thank you in advance先感谢您

EDIT: The issue is caused by the materialize framework.编辑:问题是由物化框架引起的。 I was initialising the select element BEFORE adding options to it.在添加选项之前,我正在初始化 select 元素。 Check my answer for details.查看我的答案以获取详细信息。

The issue has been solved thanks to Sean Doherty at the Materialize Gitter channel.多亏了 Materialize Gitter 频道的 Sean Doherty,这个问题已经得到解决。

The problem occurred because I was initialising the M select element BEFORE the dynamic population.出现问题是因为我在动态填充之前初始化了 M select 元素。

Here follows the correct以下是正确的

myJS我的

<script>
 document.addEventListener('DOMContentLoaded', function() {
   $.getJSON(myJSON, function(data){
      var select = document.getElementById("myselectid");
      $.each(data.resources, function(i, field){
        select.add(new Option(field.name, field.name));
      });
      // init select element after collecting the option values
      M.FormSelect.init(document.querySelectorAll('select'));
    });
  });
  $(document).ready(function(){
     $('.sidenav').sidenav();
     // do not init select here because the dyn population is not done yet
     //$('select').formSelect();
  });
 </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM