php – 从HTML调用Javascript函数
|
我有一个包含5行(比如说)的
HTML表格,它正在网页上显示.表的第一列是RowID(每行唯一),每行的最后一列是[X]按钮(点击后将删除该行).整个表位于< div>中. id =“cartmain”的元素
<div id="cartmain">
<table>
<tr>
<td>....</td>
<td>....</td>
<td> <a href="#" onclick="removeitem('id1')"> [X] </a> </td>
</tr>
..
..
</table>
</div>
这是我删除行的方式: 第1步:当用户单击[X]按钮时,XMLHTTPRequest函数将向每行具有唯一ID的PHP代码发送消息.在PHP代码删除行的过程中,DIV元素显示“正在加载…” document.getElementById(“cartmain”).innerHTML =“Loading …” 第2步:PHP代码将从表(和数据库)中删除行,并将返回剩余的表. <?php
//removing row from database/table...
//send the remaining table back to the XMLHTTPRequest function...
echo "<table>";
echo "<tr><td>...</td>
<td>...</td>
<td> <a href="#" onclick="removeitem('id1')"> [X] </a> </td>";
echo "</tr>";
...
...
...
echo "</table>";
?>
然后,使用以下行在DIV元素中显示从PHP代码接收的剩余表: document.getElementById(“cartmain”).innerHTML = removeitem.responseText; 我的问题: 重要说明:当我刷新页面时,我可以删除另一行.但这一次,我只能删除一行.要删除一个,我必须再次刷新页面. 有人能够在这里找出问题吗?请帮忙. 注意:我的桌子实际上是一个购物车,每行包含一个产品. [X]按钮实际上表示从购物车中“删除项目”. 这是JavaScript代码: function removeitem(itemid)
{
alert("The item to be removed is: "+itemid);
if(window.XMLHttpRequest)
{
removeitem = new XMLHttpRequest();
}
else
{
removeitem=new ActiveXObject("Microsoft.XMLHTTP");
}
removeitem.onreadystatechange=function()
{
if (removeitem.readyState==4 && removeitem.status==200)
{
document.getElementById("cartmain").innerHTML=removeitem.responseText;
}
else if(removeitem.readyState < 4)
{
document.getElementById("cartmain").innerHTML="Loading...";
}
}
var linktoexecute = "cart.php?option=5&itemid="+itemid; //option =5 signifies that I want to remove the item with ITEMID.
removeitem.open("GET",linktoexecute,true);
removeitem.send();
}
函数removeitem第一次显示警告“要删除的项目是:123”,但未显示第二次.当我在Firebug控制台中检查时,第二次出现以下错误: removeitem不是一个功能 请帮忙!! 我所有的Javascript函数都在一个单独的文件(sc.js)中,我正在使用它< script type =“text / javascript”src =“js / sc.js”>< / script>在我页面的HEAD标签中. 我的观点:最后我认为这个问题很简单:如果一个网页使用XMLHTTPRequest从PHP页面请求一些HTML – 如果该HTML(由PHP发送)包含一些调用Javascript函数的按钮 – 那么在这种情况下会发生什么[?].既然我能够第一次删除该行,我认为上述情况不起作用是错误的.只是当我第二次点击按钮时,来自PHP和已经加载的Javascript的代码不知道彼此存在. 解决方法
在removeitem函数中,您将通过XMLHttpRequest对象覆盖removeitem.因此,后来的错误removeitem不是一个函数.
要解决此问题,可以使用var(推荐)前缀removeitem,或使用其他变量名称,或两者都使用. 推荐代码: function removeitem(itemid) {
alert("The item to be removed is: "+itemid);
var removeitem = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
removeitem.onreadystatechange = function() {
if (removeitem.readyState == 4 && removeitem.status == 200) {
document.getElementById("cartmain").innerHTML = removeitem.responseText;
} else if(removeitem.readyState < 4) {
document.getElementById("cartmain").innerHTML="Loading...";
}
}
var linktoexecute = "cart.php?option=5&itemid="+itemid; //option =5 signifies that I want to remove the item with ITEMID.
removeitem.open("GET",true);
removeitem.send();
}
在removeitem之前添加var可以解决问题,因为该变量是本地定义的.当省略var时,新值将附加到最近的父声明(在本例中为函数). var removeitem = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
上一行简称: var removeitem;
if (window.XMLHttpRequest) {
removeitem = new XMLHttpRequest();
} else {
removeitem = new ActiveXObject("Microsoft.XMLHTTP");
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
