Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用json在动态中转换listview静态?_Javascript_Jquery_Json_Listview_Jquery Mobile - Fatal编程技术网

Javascript 如何使用json在动态中转换listview静态?

Javascript 如何使用json在动态中转换listview静态?,javascript,jquery,json,listview,jquery-mobile,Javascript,Jquery,Json,Listview,Jquery Mobile,我使用长轮询更新jquery mobile listview,但我用这种方式编写了静态代码来更新listview: function updatePage() { if (ajaxRequest.readyState == 4) { var parsobj = jQuery.parseJSON( ajaxRequest.responseText ); console.log(ajaxRequest.responseText);

我使用长轮询更新jquery mobile listview,但我用这种方式编写了静态代码来更新listview:

 function updatePage() {
      if (ajaxRequest.readyState == 4) {

             var parsobj = jQuery.parseJSON( ajaxRequest.responseText );
             console.log(ajaxRequest.responseText);
            $("#nameobj1").empty().append(parsobj.nameobj0);
            $("#prezzoobj1").empty().append(parsobj.prezzoobj0);
            $("#nameobj2").empty().append(parsobj.nameobj1);
            $("#prezzoobj2").empty().append(parsobj.prezzoobj1);
            $("#nameobj3").empty().append(parsobj.nameobj2);
            $("#prezzoobj3").empty().append(parsobj.prezzoobj2);
            $("#nameobj4").empty().append(parsobj.nameobj3);
            $("#prezzoobj4").empty().append(parsobj.prezzoobj3);
            $("#nameobj5").empty().append(parsobj.nameobj4);
            $("#prezzoobj5").empty().append(parsobj.prezzoobj4);
            makeAjaxRequest();
      }
  }
  function makeAjaxRequest() {
      var datashowobj= {'type':"show-obj" };
      ajaxRequest = new XMLHttpRequest();
      ajaxRequest.onreadystatechange = updatePage;
      ajaxRequest.open("POST","http://localhost:8080/asta/ServletObjects", true);
      ajaxRequest.setRequestHeader("Content-Type", "application/json");
      ajaxRequest.send( JSON.stringify(datashowobj));
   }
服务器发送此json{nameobj0:w,prezzobj0:1.0,nameobj1:x,prezzobj2:60.0…}

我想放在listview中的li标记如下 `

      <li><h3 id = "nameobj1"></h3> 
      <p id="prezzoobj1"></p>
      <div data-role="controlgroup" data-type="horizontal" data-mini="true">
   <select>
    <option>$</option>
    <option>€</option>
    <option>£</option>
    <option>¥</option>
    <option>₩</option>
    <option>₹</option>
   </select>
   <input id="currency-controlgroup1" type="text" data-wrapper-class="controlgroup-  textinput ui-btn">
  <button id="bo1" class ="myclass" data-icon="plus">Piazza l'offerta</button>
  <div id="piaz1"></div> 

  </div>
     </li>`

感谢您的回答,我希望我能有一个如何实现的示例。

在您的HTML标记中,只需创建一个空列表:

var parsobj = jQuery.parseJSON( ajaxRequest.responseText );
for (var id in parsobj)
{
    if ($("#"+id).length>0)  //check if exists an element with this id
        $("#"+id).empty().append(parsobj[id]);
}
<ul id="mylist" data-role="listview"></ul>
然后在updatePage代码中,迭代数组并为数组的每个成员创建一个listitem。将这些附加到listview,告诉jQM刷新listview并增强所有子控件:

var html = '';
for (var i = 0; i < parsobj.length; i++){
    html += '<li>';
    html += '<h3>' + parsobj[i].nameobj + '</h3>';
    html += '<p>' + parsobj[i].prezzobj + '</p>';        
    html += '<div data-role="controlgroup" data-type="horizontal" data-mini="true"><select><option>$</option><option>€</option> <option>£</option><option>¥</option><option>₩</option><option>₹</option></select>';
    html += '<input id="currency-controlgroup' + i +'" type="text" data-wrapper-class="controlgroup-textinput ui-btn" />';
    html += '<button id="bo' + i +'" class ="myclass" data-icon="plus">Piazza l\'offerta</button>';
    html += '<div id="piaz' + i +'"></div></div>';
    html += '</li>';
}

//empty the listview, append new items and tell jQM to refresh
$("#mylist").empty().append(html).listview("refresh").enhanceWithin();
这是一张工作票


不知道你的意思。尝试刷新列表$myisview.listviewrefresh
var html = '';
for (var i = 0; i < parsobj.length; i++){
    html += '<li>';
    html += '<h3>' + parsobj[i].nameobj + '</h3>';
    html += '<p>' + parsobj[i].prezzobj + '</p>';        
    html += '<div data-role="controlgroup" data-type="horizontal" data-mini="true"><select><option>$</option><option>€</option> <option>£</option><option>¥</option><option>₩</option><option>₹</option></select>';
    html += '<input id="currency-controlgroup' + i +'" type="text" data-wrapper-class="controlgroup-textinput ui-btn" />';
    html += '<button id="bo' + i +'" class ="myclass" data-icon="plus">Piazza l\'offerta</button>';
    html += '<div id="piaz' + i +'"></div></div>';
    html += '</li>';
}

//empty the listview, append new items and tell jQM to refresh
$("#mylist").empty().append(html).listview("refresh").enhanceWithin();