layui select 下拉框 动态添加下拉选项

html代码

<div class="layui-form-item layui-inline">
    <label class="layui-form-label">选择平台</label>
    <div class="layui-input-inline">
        <select name="plat" id="plat" lay-search>
            <option value=""></option>
        </select>
    </div>
</div>


js方法

//layui select 下拉框 动态添加下拉选项
window.getPlatList = function(){
  let loading = layer.load()
  $.ajax({
    url: '/api/plat/ls',
    dataType: 'json',
    contentType: 'application/json',
    type: 'get',
    success: function (result) {
      layer.close(loading)
      if (result.success) {
        let plat = result.data;
        plat.forEach(function(v){
          if(v.account && v.account.length>0){
              $('#plat').append(new Option(v.account, v.id));
          }
        });
        //这一句是关键
        layui.form.render("select");
      } else {
        popup.failure(result.msg)
      }
    }
  })
}