Php 在Firefox中传递HTML表行数据无法正常工作
我已经使用HTML/JS大约一周了 我使用XMLHttpRequest在主页上加载一个表,允许用户用新数据刷新表。从我创建的表中,我正在将其他数据检索到表单上的第二个XMLHttpRequest中。我通过表行的onclick事件传递数据。以下是基本代码:Php 在Firefox中传递HTML表行数据无法正常工作,php,html,dom,Php,Html,Dom,我已经使用HTML/JS大约一周了 我使用XMLHttpRequest在主页上加载一个表,允许用户用新数据刷新表。从我创建的表中,我正在将其他数据检索到表单上的第二个XMLHttpRequest中。我通过表行的onclick事件传递数据。以下是基本代码: function test(tablerow) { //get the rowindex for the row var myIndex = tablerow.rowIndex; //get the cell data for the cl
function test(tablerow)
{
//get the rowindex for the row
var myIndex = tablerow.rowIndex;
//get the cell data for the clicked row in the first column
// original var company_id = table1.rows.item(myIndex).cells.item(0).textContent;
var my_id = table1.rows.item(myIndex).cells.item(0).textContent;
alert(my_id);
}
当我第一次加载表时,它工作得很好,但是当我加载一个新表时,它似乎在保存以前的表数据
例如:
第一张表:
ID
--
1
2
3
4
5
单击该行时,响应会正确地获取ID。但是当我加载第二个表时:
ID
--
6
7
8
9
10
当单击第1行时,我得到1作为单元格数据的响应。2、第二排等。。。它获取原始表数据的响应。如前所述,这在Chrome或IE中不会发生。我得到了正确的数据
我创建了一个小测试用例,它展示了我看到的行为
有人知道如何解决这个问题吗?为什么这只发生在Firefox中
测试用例:
Page1.php
<!DOCTYPE html>
<html>
<script type="text/javascript">
function test(tablerow)
{
//get the rowindex for the row
var myIndex = tablerow.rowIndex;
//get the cell data for the clicked row in the first column
// original var company_id = table1.rows.item(myIndex).cells.item(0).textContent;
var my_id = table1.rows.item(myIndex).cells.item(0).textContent;
alert(my_id);
}
function getPage()
{
HttpRequest = PostXmlHttpObject();
HttpRequest.onreadystatechange=function()
{
if (HttpRequest.readyState==4 && HttpRequest.status==200)
{
document.getElementById("divTable").innerHTML=HttpRequest.responseText;
}
}
HttpRequest.open("GET","page2.php?table="+1, true);
HttpRequest.send();
}
function getPage2()
{
HttpRequest = PostXmlHttpObject();
HttpRequest.onreadystatechange=function()
{
if (HttpRequest.readyState==4 && HttpRequest.status==200)
{
document.getElementById("divTable").innerHTML=HttpRequest.responseText;
}
}
HttpRequest.open("GET","page2.php?table="+2, true);
HttpRequest.send();
}
function PostXmlHttpObject()
{
var HttpRequest = false;
if(window.XMLHttpRequest)
{ // Mozilla, Safari,...
HttpRequest = new XMLHttpRequest();
if(HttpRequest.overrideMimeType)
{
// set type accordingly to anticipated content type
HttpRequest.overrideMimeType('text/html');
}
}
else if(window.ActiveXObject)
{ // IE
try{
HttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
HttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
if(!HttpRequest)
{
alert('Cannot create XMLHTTP instance');
return false;
}
return HttpRequest;
}
</script>
<body>
<form name="form1" id="form1">
<input type="button" value="Get XML Page 1" onclick="getPage()"/>
<input type="button" value="Get XML Page 2" onclick="getPage2()"/>
<br /><br />
<div id="divTable"><b></b></div>
</form>
</body>
</html>
功能测试(tablerow)
{
//获取该行的行索引
var myIndex=tablerow.rowIndex;
//获取第一列中单击行的单元格数据
//原始var company_id=table1.rows.item(myIndex).cells.item(0).textContent;
var my_id=table1.rows.item(myIndex).cells.item(0).textContent;
警报(我的身份证);
}
函数getPage()
{
HttpRequest=PostXmlHttpObject();
HttpRequest.onreadystatechange=函数()
{
if(HttpRequest.readyState==4&&HttpRequest.status==200)
{
document.getElementById(“divTable”).innerHTML=HttpRequest.responseText;
}
}
打开(“GET”,“page2.php?table=“+1,true”);
HttpRequest.send();
}
函数getPage2()
{
HttpRequest=PostXmlHttpObject();
HttpRequest.onreadystatechange=函数()
{
if(HttpRequest.readyState==4&&HttpRequest.status==200)
{
document.getElementById(“divTable”).innerHTML=HttpRequest.responseText;
}
}
打开(“GET”,“page2.php?table=“+2,true”);
HttpRequest.send();
}
函数PostXmlHttpObject()
{
var-HttpRequest=false;
if(window.XMLHttpRequest)
{//Mozilla、Safari,。。。
HttpRequest=新的XMLHttpRequest();
if(HttpRequest.overrideMimeType)
{
//根据预期的内容类型设置类型
HttpRequest.overrideMimeType('text/html');
}
}
else if(window.ActiveXObject)
{//IE
试一试{
HttpRequest=新的ActiveXObject(“Msxml2.XMLHTTP”);
}
捕获(e)
{
尝试
{
HttpRequest=新的ActiveXObject(“Microsoft.XMLHTTP”);
}捕获(e){}
}
}
如果(!HttpRequest)
{
警报(“无法创建XMLHTTP实例”);
返回false;
}
返回HttpRequest;
}
Page2.php
身份证件
1.
2.
3.
4.
5.
身份证件
6.
7.
8.
9
10
像这样修改测试函数并检查
function test(tablerow)
{
alert(tablerow.cells.item(0).innerHTML);
alert(tablerow.cells[0].innerHTML);
}
这似乎很有效。两个警报都使用相同的编号和正确的列数据进行响应。谢谢。你知道为什么Firefox不喜欢前面的代码,但IE和Chrome喜欢吗?Firefox是否将表数据缓存在某个地方,所以当我引用表1中的数据时,它使用了旧数据?这是我的猜测,但我想知道为什么IE和Chrome表现不同。
function test(tablerow)
{
alert(tablerow.cells.item(0).innerHTML);
alert(tablerow.cells[0].innerHTML);
}