ajax响应导致html问题

时间:2013-09-25 13:03:19

标签: javascript html ajax

我正在处理一个表单而且我发送ajax调用dropdown onchange每件事都没问题,但唯一让我无法理解的是当我在响应div中显示html(响应)所有的tr和td消失了肆无忌惮地试图找出原因,但一切都是徒劳。

id为txtHint的div是一个响应div,我想用<tr>....</tr> ajax响应替换<tr>....</tr>给我回复

 <div id='txtHint'>
    <?php while($rows = mysql_fetch_array($select_recrds)){?>            
         <tr>
            <td field="itemid" width="80" sortable="true"><a href=""><?php  echo $rows['ee'];?></a></td>
            <td field="productid" width="100" sortable="true"><?php  echo $rows['ss'];?></td>
            <td field="listprice" width="80" align="right" sortable="true"><?php  echo $rows['cc'];?></td>
            <td field="unitcost" width="80" align="right" sortable="true"><?php  echo $rows['bb'];?></td>
            <td field="status" width="160" align="center"><?php  echo $rows['xx'];?></td>
        </tr>        
   <?php }?>
 </div>

这是ajax响应

 while($row = mysql_fetch_array($result))
 {

       echo  '<tr>';
           echo       '<td field="itemid" width="80" sortable="true"><a href="">'.$row["ee"].'</a></td>';
           echo       '<td field="productid" width="100" sortable="true">'.$row["ss"].'</td>';
           echo       '<td field="listprice" width="80" align="right" sortable="true">'.$row["cc"].'</td>';
           echo       '<td field="unitcost" width="80" align="right" sortable="true">'.$row["bb"].'</td>';
           echo       '<td field="status" width="160" align="center">'.$row["xx"].'</td>';
           echo       '</tr>';
 }

两者看起来都是一样的,但唯一不同的是它包含的记录,所以不要担心它。

当我提醒回复html时,如下图所示:

enter image description here

但我收到的回复div是

  <div id="txtHint"><a href="">12</a>Bolt-Ons2013-09-25 14:28:520000-00-00 00:00:00in progress<a href="">4</a>Bolt-Ons2013-09-25 14:28:470000-00-00 00:00:00in progress</div>

为什么tr和tds会消失,我该如何解决这个问题?

以下是我的ajax电话

<script>
function showUser(str)
{
    if (str=="")
    {
      document.getElementById("txtHint").innerHTML="";
      return;
    } 
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
        alert(xmlhttp.responseText);
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
      }
    }
    xmlhttp.open("GET","filter_data.php?q="+str,true);
    xmlhttp.send();
}
</script>

1 个答案:

答案 0 :(得分:0)

为了帮助您进行调试,请尝试将结果放在如下变量中:

 while($row = mysql_fetch_array($result))
 {

 $var.='<tr>';
 $var.='<td field="itemid" width="80" sortable="true"><a href="">'.$row["order_id"].'</a></td>';
 $var.='<td field="productid" width="100" sortable="true">'.$row["order_type"].'</td>';
 $var.='<td field="listprice" width="80" align="right" sortable="true">'.$row["order_created"].'</td>';
 $var.='<td field="unitcost" width="80" align="right" sortable="true">'.$row["order_updated"].'</td>';
 $var.='<td field="status" width="160" align="center">'.$row["status"].'</td>';
 $var.='</tr>';
 }

然后,制作

mail('yourmail@mail.com', 'test', $var);

在邮件中查看结果。

在您的Html代码中,执行以下操作:

<table>
    <tbody id='txtHint'>

    </tbody>
</table>