Jquery输出未显示在网页中

时间:2012-11-22 15:14:02

标签: jquery

我正在尝试在网页上显示数据库查询的结果。我正在使用下面的JQUERY脚本通过php脚本将数据作为JSON数组获取。

出于某种原因,我无法弄清楚,网页。是空白的。请注意,警报显示数据正在正确传递给$ .each循环。

谁能告诉我我做错了什么?

感谢
克里斯

JQUERY SCRIPT

function projectData() {

var data = '';

    $.ajax(
    {
        url:"projectdata.php", 
        type: "POST",
        dataType: 'JSON',
        cache: false,
        success:function(data)
        {
            $( "#projectList" ).insertAfter('<tbody>' + 

                $.each(data, function(indx, value) 
                {
                    '<tr><td>' + data[indx].project_id + '</td>' +
                    '<td>' + data[indx].dept + '</td>' +
                    '<td>' + value.projName + '</td>' +
                    '<td>' + data[indx].notes + '</td>' + 
                    '<td>' + data[indx].dates + '</td>' +
                    '<td>' + data[indx].status + '</td>' +
                    alert("ID: " + value.project_id + "   " + "DEPT: " + data[indx].dept);
                } //end function
                ) //end each
                ) //end After

        } //end success
    }); //end ajax

} //结束projectdata


HTML SCRIPT

  

现有项目:

&lt; table id="projectList">    
&lt; thead>  
&lt; tr class="success">   
&lt; th class="span1">PROJECT ID &lt; /th>  
&lt; th class="span2">SCHOOL / DEPT &lt; /th>  
&lt; th class="span1">PROJECT &lt; /th>  
&lt; th class="span3">DESCRIPTION / PROJECT NOTES &lt; /th>  
&lt; th class="span1">APPROX DATES &lt; /th>  
&lt; th class="span1">STATUS &lt; /th>  
&lt; th class="span1">USER</th> &lt; /tr>  
&lt; /thead>  
&lt; tbody>  
&lt; !-- RESULTS FROM JQUERY/PHP GOES HERE -->  


&lt; /tbody>  
&lt; /table>

3 个答案:

答案 0 :(得分:3)

您有语法错误

$( "#projectList" ).insertAfter('<tbody>' + 

$.each(data, function(indx, value) 
{
   '<tr><td>' + data[indx].project_id + '</td>' +

这没有任何意义

也许这就是你想要做的事情

var result = "";
$.each(data, function(indx, value) 
{
   result += '<tr><td>' + data[indx].project_id + '</td>' +
                '<td>' + data[indx].dept + '</td>' +
                '<td>' + value.projName + '</td>' +
                '<td>' + data[indx].notes + '</td>' + 
                '<td>' + data[indx].dates + '</td>' +
                '<td>' + data[indx].status + '</td></tr>';
}
$("#projectlist").insertAfter('<tbody>' + result + '</tbody>');

答案 1 :(得分:0)

'<tbody>' + $.each(data, function(indx, value) 

在此代码中,您将字符串与函数

连接起来

尝试先使用$ .each创建html,然后将其放入tbody

var result = "";

$.each(data, function(i, val){

   result += "<tr><td>" + val.project_id + "</td></tr>";

})

$( "#projectList tbody" ).html(result);

答案 2 :(得分:0)

将insertAfter更改为追加:

           $( "#projectList" ).append('<tbody>' + 

                '<tr><td>' + data[indx].project_id + '</td>' +
                '<td>' + data[indx].dept + '</td>' +
                '<td>' + value.projName + '</td>' +
                '<td>' + data[indx].notes + '</td>' + 
                '<td>' + data[indx].dates + '</td>' +
                '<td>' + data[indx].status + '</td>' 
            );

那里的警报是什么?没有得到身份。