2013-03-17 69 views
1

我想在動態列表內使用jQuery手機創建動態列表 我試圖使用item.On("click", callGroups);但我無法將參數傳遞給該函數。 我使用jQuery Mobile的1.3和jQuery 1.8阿賈克斯項目動態列表點擊創建新的子列表

,這是我的Ajax代碼

<script type="text/javascript" type="text/javascript"> 


     $(document).ready(function() { 

      Greating(); 
     }); 

     // $(document).on('pageinit', function() { 
     function callGroups(ID) 
     { 
     $("#"+ID).append("<ul><li> first</li><li>second item</li> </a></li> "); 


     } 

     function Greating() { 

      $.ajax({ 

       type: "POST", 
       url: "CarService.asmx/GetAllsections", 
       data: "{}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (response) { 

        var sections = response.d; 
        $('#page_body').append("<div data-role='page' id= 'ssaadd'><div>"); 
        $.mobile.initializePage(); 

        $.each(sections, function (index, section) { 

         // asd = "listsec" + section.secId; 
         $("#theList").append("<li> <a id = '"+section.secId+"' ><img src='pic/" + section.SecImg + "'> <br /> " + section.SecName + ' ' + section.SecArbName + " <br />" + section.SecArbDiscr + "").on("click", callGroups(); 


        }); 



        console.log(response); 
       }, 

       error: function (response) { 
        $("#theList").append("<li>error<li>"); 

        console.log(response); 
       } 
      }); 
     } 
    </script> 

如何建立內部動態列表,動態列表?

回答

0

這裏是我想這段代碼的答案,它是工作

$.each(sections, function (index, section) { 

         var asd = section.SecName; 
         $("#theList").append("<li id='" + asd + "'> <img src='pic/" + section.SecImg + "'> <br /> " + section.SecName + ' ' + section.SecArbName + " <br />" + section.SecArbDiscr + ""); 
         $("#"+asd).append("<ul><li > <img src='pic/" + section.SecImg + "'> <br /> " + section.SecName + ' ' + section.SecArbName + " <br />" + section.SecArbDiscr + "</li></ul></li>"); 
        }); 
2

您可以附加的方法是錯誤的,要添加的click事件處理程序的循環,而不是新增li#theList,您可以使用appendTo方法來修改你的代碼

$("<li> <a id = '"+section.secId+"' ><img src='pic/" + section.SecImg + "'> <br /> " + section.SecName + ' ' + section.SecArbName + " <br />" + section.SecArbDiscr + "").appendTo('#theList').on("click", callGroups); 

和更改呼叫羣組到

function callGroups(ID) { 
    $(this).append("<ul><li> first</li><li>second item</li> </a></li> "); 
} 
+0

它正在工作,但它不斷在同一頁面中添加新列表,我希望它被隱藏,當我點擊該項目時,新列表生成並在新頁面中打開 – 2013-03-17 17:47:13