首页 > 解决方案 > w3.js 中的信息阅读困难

问题描述

在 w3.js 中

    {
"customers":[
{"CustomerName" : "Alfreds Futterkiste","City" : "Berlin","Country" : "Germany"},
{"CustomerName" : "Berglunds snabbköp","City" : "Luleå","Country" : "Sweden"},
]}


<select id="id01">
  <option w3-repeat="customers">{{CustomerName}}</option>
</select>



<script>
w3.getHttpObject("customers.js", myFunction);

function myFunction(myObject) {
  w3.displayObject("id01", myObject);
}
</script>

但我的对象没有任何名称,例如(客户),并且是:

[{"id":12,"name":"hamwrew"},{"id":13,"name":"mamad2"}]

如何显示我的对象?在 html 或 jsp 页面中

标签: javascriptw3.js

解决方案


你没有一个对象,而是一个数组,所以首先从你的数组创建一个对象:

var dataArray = [{"id":12,"name":"hamwrew"},{"id":13,"name":"mamad2"}];
var nameList = { names: dataArray };

所以:

    <select id="id01">   
      <option w3-repeat="names" value={{id}}>{{name}}</option>
    </select>

    <script>
      w3.getHttpObject("customers.js", myFunction);

      function myFunction(myArray) {   
        var nameList = { names: myArray};   
        w3.displayObject("id01", nameList);
      }
   </script>

推荐阅读