在循环中动态创建jquery mobile链接

在循环中动态创建jquery mobile链接,jquery,jquery-mobile,each,Jquery,Jquery Mobile,Each,考虑这个json对象: [{"data":{"ID":"3","Category":"Career"}},{"data":{"ID":"5","Category":"Emotions"}},{"data":{"ID":"1","Category":"Finance"}},{"data":{"ID":"2","Category":"Health"}},{"data":{"ID":"6","Category":"Legacy"}},{"data":{"ID":"4","Category":"Rela

考虑这个json对象:

[{"data":{"ID":"3","Category":"Career"}},{"data":{"ID":"5","Category":"Emotions"}},{"data":{"ID":"1","Category":"Finance"}},{"data":{"ID":"2","Category":"Health"}},{"data":{"ID":"6","Category":"Legacy"}},{"data":{"ID":"4","Category":"Relationships"}}]  

并考虑此客户端代码:

$(function (){ 

 $.ajax({                                      
  url: 'category.php',                         
  data: "",                        
   dataType: 'json',                      
  success: function(data)         
  {
     var output = '';
      $.each( data, function( key, obj) {
      $.each( obj, function( index, item) {
        //console.log(index+ ": " + item);
        url = "goal.php?catID=" + item.ID;
        output = '<li><a>' + item.Category+ '</a></li>';
        href = $('a').attr("href",url); $('a').append(href);
        $('#listview').append(output).listview('refresh');
       });
   });
   } 
});  }); 
$(函数(){
$.ajax({
url:'category.php',
数据:“,
数据类型:“json”,
成功:功能(数据)
{
var输出=“”;
$。每个(数据、功能(键、obj){
$。每个(对象、功能(索引、项目){
//console.log(索引+”:“+项);
url=“goal.php?catID=“+item.ID;
输出=“
  • ”+项。类别+”
  • ; href=$('a').attr(“href”,url);$('a').append(href); $('#listview')。追加(输出)。listview('refresh'); }); }); } }); });

    
    我的目标
    

    我在JQuery mobile中的第一次尝试,我正在尝试循环JSON并输出类别及其链接-

    我几乎成功了,但它只是在url上添加了最后一个记录item.ID

    有人能帮忙吗

    谢谢你这么做

    $('a').attr("href",url)
    
    您正在更改页面的所有
    a
    元素。最后一次迭代当然会赢

    我建议你在你的循环中:

    $('#listview').append(
        $('<li/>').append($('</a>').attr('href', url).html(url))
    ).listview('refresh');
    
    $('#listview')。追加(
    $('
  • ').append($('').attr('href',url).html(url)) ).listview(“刷新”);
  • 当您这样做时

    $('a').attr("href",url)
    
    您正在更改页面的所有
    a
    元素。最后一次迭代当然会赢

    我建议你在你的循环中:

    $('#listview').append(
        $('<li/>').append($('</a>').attr('href', url).html(url))
    ).listview('refresh');
    
    $('#listview')。追加(
    $('
  • ').append($('').attr('href',url).html(url)) ).listview(“刷新”);
  • 试试看

    $(函数(){
    $.ajax({
    url:'category.php',
    数据:“,
    数据类型:“json”,
    成功:功能(数据)
    {
    $。每个(数据、功能(键、obj){
    var项目=对象数据;
    $('
  • ')。请追加($('

    尝试

    $(函数(){
    $.ajax({
    url:'category.php',
    数据:“,
    数据类型:“json”,
    成功:功能(数据)
    {
    $。每个(数据、功能(键、obj){
    var项目=对象数据;
    
    $('
  • ')。追加($('

    谢谢-此解决方案可以工作并指出我的错误所在,但Arun p Johny的第二个解决方案更清晰,因此接受。谢谢-此解决方案可以工作并指出我的错误所在,但Arun p Johny的第二个解决方案更清晰,因此接受。