jquery ajax函数在成功时生成动态内容

时间:2012-12-21 03:36:20

标签: javascript jquery html css ajax

我有function迭代一个array,我想生成所有te div,其中来自array的信息应该显示...萤火虫检查员显示divs在那里,但图像和信息没有显示。我非常感谢你们给我的任何帮助,这是我的代码

        $("#busca").on('click', function(){

        $.ajax({ 
        type    : "POST",
        url     : "service.php",
        dataType: "json",
        data    : {
            action:"fullProjects",
            },          
            success:function(data){

                var i;
                for (i = 0; i < data.data.length; ++i) {

                    console.log(i);

                    var divCreator ='';

                    divCreator+='<div id="grupo'+i+'" class="typeface-js" style="font-family:GreyscaleBasic">';
                    divCreator+='<div class="tipo-pro">';
                    divCreator+='<div id="tipo_'+i+'" class="tipo"></div>';
                    divCreator+='<div id="tipo_arq_abajo'+i+'" class="abajo"></div>';
                    divCreator+='</div>';
                    divCreator+='<div id="fotoproyectos'+i+'" class="foto"></div>';
                    divCreator+='<div id="nombreproyectos'+i+'" class="nombre-pro"></div>';
                    divCreator+='</div>';
                    divCreator+='</div><br><br><br><br><br><br>';

                    $("#contiene-pro").append(divCreator);

                    //$("#fotoproyectos"+i).empty();
                    var htmlThings = '';
                    htmlThings += '<img src="' + data.data[i].path + '" height="128" width="160">';
                    $("#fotoproyectos"+i).append(htmlThings);

                    $("#nombreproyectos"+i).empty();
                    var htmlMoreThings = '';
                    htmlMoreThings += "<form method=\"post\" name=\"projectsearch\" id=\"projectsearch"+ i +"\" action=\"proyectos_arq.php\">" 
                    htmlMoreThings += "<span style=\"cursor: pointer;\" onclick=\"document.getElementById('projectsearch"+ i +"').submit()\">"+ data.data[i].projectName +"</span>"
                    //html2 += "<button id=" + "button" + data.projectId + " style=\"visibility:hidden;\"><span id=" + data.projectId + " style=\"cursor: pointer;\"><span>" + data.projectName + "</span></span></button>"
                    htmlMoreThings += "<input name=\"project_id\" type=\"hidden\" id=\"project_id\" value=" + data.data[i].projectId + ">"  
                    htmlMoreThings += "</form>"
                    $("#nombreproyectos"+i).append(htmlMoreThings);


                };

            }
        })

    });

contiene-pro是其余div's的主要容器。所有帮助都非常感谢...我尝试了不同的方法,结果相同

$("#busca").on('click', function(){

        $.ajax({ 
        type    : "POST",
        url     : "service.php",
        dataType: "json",
        data    : {
            action:"fullProjects",
            },          
            success:function(data){

                var i;
                for (i = 0; i < data.data.length; ++i) {

                    console.log(i);

                    var divCreator ='';

                    divCreator+='<div id="grupo'+i+'" class="typeface-js" style="font-family:GreyscaleBasic">';
                    divCreator+='<div class="tipo-pro">';
                    divCreator+='<div id="tipo_'+i+'" class="tipo"></div>';
                    divCreator+='<div id="tipo_arq_abajo'+i+'" class="abajo"></div>';
                    divCreator+='</div>';
                    divCreator+='<div id="fotoproyectos'+i+'" class="foto"><img src="' + data.data[i].path + '" height="128" width="160"></div>';
                    divCreator+='<div id="nombreproyectos'+i+'" class="nombre-pro"><form method=\"post\" name=\"projectsearch\" id=\"projectsearch"+ i +"\" action=\"proyectos_arq.php\">';
                    divCreator+='<span style=\"cursor: pointer;\" onclick=\"document.getElementById('projectsearch"+ i +"').submit()\">"+ data.data[i].projectName +"</span>'
                    divCreator+='<input name=\"project_id\" type=\"hidden\" id=\"project_id\" value=" + data.data[i].projectId + ">';
                    divCreator+='</form></div>';
                    divCreator+='</div>';
                    divCreator+='</div><br><br><br><br><br><br>';

                    $("#contiene-pro").append(divCreator);


                };

            }
        })

    });

1 个答案:

答案 0 :(得分:0)

由于“&amp;”不匹配,您的代码错误太多。修正:

$("#busca").on('click', function() {

  $.ajax({
    type : "POST",
    url : "service.php",
    dataType : "json",
    data : {
      action : "fullProjects",
    },
    success : function(data) {

      var i;
      for ( i = 0; i < data.data.length; ++i) {

        console.log(i);
        var thisData = data.data[i];
        var divCreator = '';

        divCreator += '<div id="grupo' + i + '" class="typeface-js" style="font-family:GreyscaleBasic">';
        divCreator += '<div class="tipo-pro">';
        divCreator += '<div id="tipo_' + i + '" class="tipo"></div>';
        divCreator += '<div id="tipo_arq_abajo' + i + '" class="abajo"></div>';
        divCreator += '</div>';
        divCreator += '<div id="fotoproyectos' + i + '" class="foto"><img src="' + thisData.path + '" height="128" width="160"></div>';
        divCreator += '<div id="nombreproyectos' + i + '" class="nombre-pro"><form method="post" name="projectsearch" id="projectsearch' + i + '" action="proyectos_arq.php">';
        divCreator += '<span style="cursor: pointer;" onclick="document.getElementById("projectsearch' + i + '").submit()">' + thisData.projectName + '</span>';
        divCreator += '<input name=\"project_id\" type=\"hidden\" id=\"project_id\" value="' + thisData.projectId + '">';
        divCreator += '</form></div>';
        divCreator += '</div>';
        divCreator += '<br><br><br><br><br><br>';
        $("#contiene-pro").append(divCreator);
      };
    }
  });
});