简体   繁体   English

元素未从DOM中删除

[英]element not being removed from DOM

I have code to delete a record from mysql, displayed in a table via php, and subsequently delete the table row from the page. 我有代码从mysql中删除一条记录,通过php在表中显示,然后从页面中删除表行。 The record is deleted, however nothing changes in the page or the DOM, and it should change instantly. 记录被删除,但是页面或DOM中没有任何更改,它应该立即更改。

Here is the javascript code to delete from the DOM 这是要从DOM中删除的JavaScript代码

function deleteItem(layer, url) {
    var xmlHttp=GetXmlHttpObject();
    if(xmlHttp==null) {
        alert("Your browser is not supported?");
    }
    xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
                if(xmlHttp.responseText == 'result=true') {
                        var row = document.getElementById(layer);
                        row.parentNode.removeChild(row);
                }

        } else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading") {
                document.getElementById(layer).innerHTML="loading";
        }
    }
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}
function deleteRec(layer, pk) {
    url = "get_records.php?cmd=deleterec&pk="+pk+"&sid="+Math.random();
    if (confirm("Confirm Delete?")) {
        deleteItem(layer, url);
    }
}

Which is called from php like so: 像这样从php调用:

echo '<tr id=\"article_' . $pk . '\">' . "\n";   
echo '<td><a href="#" onclick="deleteRec(\'article_' . $pk .'\', \'' . $pk . '\')">DELETE</a></td>' . "\n"; 

$pk is the primary key of the record. $ pk是记录的主键。

The resultant html is fine(obviously since the record is deleted) 生成的html很好(显然是因为删除了记录)

<a href="#" onclick="deleteRec('article_260343387801', '260343387801')">DELETE</a>

But the page is not updated in any way. 但是页面不会以任何方式更新。

Why? 为什么?

I just threw your code into a test page and it works for me when I remove the: 我只是将您的代码放入测试页,当我删除以下代码时,它对我有用:

 document.getElementById(layer).innerHTML=xmlHttp.responseText;

line that's just before the line: 在该行之前的行:

} else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading") {

The document.getElementById(layer).innerHTML=xmlHttp.responseText; document.getElementById(layer).innerHTML=xmlHttp.responseText; is causing an error. 导致错误。

Edit: I'll just add that my 'version' of get_records.php is simply a php page the echos 'result=true' for my own testing purposes, so if you're still running into issues then I would suggest that your PHP script, while deleting the correct data, isn't returning the correct string - you should check what actually gets output to xmlHttp.responseText 编辑:我只是添加我的get_records.php “版本”只是一个php页面,出于我自己的测试目的,回显“ result = true”,因此,如果您仍然遇到问题,那么我建议您使用PHP脚本在删除正确的数据时未返回正确的字符串-您应检查实际输出到xmlHttp.responseText

Edit 2: Your PHP code never actually returns 'result=true' in a manner which your responseText will recognise. 编辑2:您的PHP代码从不会以responseText可以识别的方式实际返回'result = true'。 You have: 你有:

if($cmd=="deleterec") {
    mysql_query('DELETE FROM AUCTIONS WHERE ARTICLE_NO = ' . $pk);
}

so you will delete the correct item but you have no echo 'result=true'; 因此您将删除正确的项目,但没有echo 'result=true'; anywhere so your if statement checking for responseText is never going to get called. 任何地方,因此您的if语句检查responseText永远不会被调用。

EDIT 3: My current test code (which works fine in firefox [untested in other browsers]). 编辑3:我当前的测试代码(在firefox中可以正常使用[在其他浏览器中未测试过])。

<script type="text/javascript">
    var xmlHttp;

    function GetXmlHttpObject(){
        var objXMLHttp = null;

        if (window.XMLHttpRequest){
        try{
            objXMLHttp = new XMLHttpRequest();
        }catch (e){
            objXMLHttp = false;
        }
    }else if (window.createRequest){
        try{
            objXMLHttp = new window.createRequest();
        }catch (e){
            objXMLHttp = false;
        }
    }else if (window.ActiveXObject){
        try {
            objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            try {
                objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e){
                objXMLHttp = false;
            }
        }
    }

    return objXMLHttp;
  }
 function deleteItem(layer, url) {
 var xmlHttp=GetXmlHttpObject();
 if(xmlHttp==null) {
    alert("Your browser is not supported?");
 }
 xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
            console.log(xmlHttp.responseText);
            if(xmlHttp.responseText == 'result=true') {
                    var row = document.getElementById(layer);
                    row.parentNode.removeChild(row);
            }
            //document.getElementById(layer).innerHTML=xmlHttp.responseText;
    } else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading") {
            //document.getElementById(layer).innerHTML="loading";
    }
 }
 xmlHttp.open("GET",url,true);
 xmlHttp.send(null);
 }
 function deleteRec(layer, pk) {
    url = "get_records.php?cmd=deleterec&pk="+pk+"&sid="+Math.random();
    if (confirm("Confirm Delete?")) {
    deleteItem(layer, url);
    }
 }
</script>


<table>
      <tr id="article_260343387801">
      <td><a href="#" onclick="updateByPk()">Name</a></td>
      <td><a href="#" onclick="deleteRec('article_260343387801', '260343387801')">DELETE</a>
      </td>
      </tr>
 </table>

This works fine in combination with the php code (with the database connection/query stuff commented out for my personal testing). 与php代码结合使用时效果很好(数据库连接/查询内容已注释掉,供我进行个人测试)。

I don't immediately see why: 我不立即明白为什么:

row.parentNode.removeChild(row);

wouldn't work... are you sure you're actually getting there? 不行...您确定要到达那儿吗? Your error reporting is problematic: 您的错误报告存在问题:

document.getElementById(layer).innerHTML=xmlHttp.responseText;

'layer' is a <tr>, and you can't assign innerHTML on a <tr> in IE. “图层”是一个<tr>,您不能在IE中的<tr>上分配innerHTML。 Instead, you will get an 'Unknown runtime error' exception and the script will stop. 相反,您将获得“未知的运行时错误”异常,并且脚本将停止。

(Plus, it's undefined what would happen if you tried to put text directly inside a <tr> without a <td> around it.) (此外,如果您尝试将文本直接放在<tr>内而周围没有<td>,将会发生什么情况。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM