从 HTML 调用 Javascript 函数

发布于 2024-12-18 02:13:07 字数 3281 浏览 0 评论 0原文

我有一个包含 5 行(比如说)的 HTML 表格,它显示在网页上。表格的第一列是RowID(每行都是唯一的),每行的最后一列是[X]按钮(单击该按钮将删除排)。整个表格位于带有 id="cartmain"

元素中,

<div id="cartmain">
     <table>
          <tr>
            <td>....</td>
            <td>....</td>
            <td> <a href="#" onclick="removeitem('id1')"> [X] </a> </td>
          </tr>
          ..
          ..
     </table>
</div>

这就是我删除行的方式:

STEP1:当用户单击[X]按钮时,XMLHTTPRequest函数将向PHP代码发送消息,该消息具有每行的唯一ID。在 PHP 代码删除行期间,DIV 元素显示“正在加载...”

document.getElementById("cartmain").innerHTML = "Loading..."

STEP2: 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>";
?>

然后,从 PHP 代码接收到的剩余表格将通过使用以下行显示在 DIV 元素中:

document.getElementById("cartmain").innerHTML = removeitem.responseText;

我的问题:< /强> 假设我有 5 行的表。当我单击“删除”按钮时,该行已成功删除,然后显示表格,其中包含剩余 4 行。问题来了:当我想从表中删除另一行时:什么也没有发生。换句话说,如果我再次单击某行的[X],那么什么也不会发生。未调用 XMLHTTPRequest 函数。在理想情况下,它应该使用唯一的 RowID 再次调用 XMLHTTPRequest 函数,然后表应该显示剩余的 3 行。

重要提示:当我刷新页面时,我可以删除另一行。但这一次,我也只能删除一行。要再删除一个,我必须再次刷新页面。

有人能找出这里的问题吗?请帮忙。

注意:我的表实际上是一个购物车,每行都包含一个产品。 [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 is not a function

请帮忙!!

我的所有 Javascript 函数都位于一个单独的文件 (sc.js) 中,我正在使用该文件 在我的页面的 HEAD 标记中。

我的观点:所以最后我认为问题很简单:如果一个网页使用 XMLHTTPRequest 从 PHP 页面请求一些 HTML ——并且如果该 HTML(由 PHP 发送)包含一些调用 Javascript 函数的按钮 ——那么在这种情况下会发生什么[?]。现在,由于我能够第一次删除该行,我认为说上述情况行不通是错误的。只是当我第二次单击按钮时,来自 PHP 的代码和已加载的 Javascript 不知道彼此的存在。

I am having a HTML table with 5 rows (say) which is being displayed on the webpage. The first column of the table is RowID (which is unique for each row) and the last column of each row is a [X] button (which on click will remove the row). The whole table is in a <div> element with id="cartmain"

<div id="cartmain">
     <table>
          <tr>
            <td>....</td>
            <td>....</td>
            <td> <a href="#" onclick="removeitem('id1')"> [X] </a> </td>
          </tr>
          ..
          ..
     </table>
</div>

This is how I am removing the row:

STEP1: When user clicks on [X] button, an XMLHTTPRequest function will send message to a PHP code with unique id of each row. During the time PHP code is removing the row, DIV element shows "Loading..."

document.getElementById("cartmain").innerHTML = "Loading..."

STEP2: PHP code will delete the row from table (and from database) and will return the remaining table.

<?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>";
?>

This remaining table received from the PHP code is then shown in the DIV element by using the below line:

document.getElementById("cartmain").innerHTML = removeitem.responseText;

My Problem:
Suppose I have table with 5 rows. When I click on the Remove button, the row is removed successfully and table is then displayed with 4 remaining rows. Here comes the problem: When I want to remove another row from the table: Nothing is Happening. In other words, if I again click on [X] of some row then nothing happens. The XMLHTTPRequest function is not called. In ideal case, it should have called the XMLHTTPRequest function again with that unique RowID and table should then be displayed with remaining 3 rows.

IMPORTANT NOTE: When I refresh the page, then I am able to remove another row. But this time also, I can remove only one row. For removing one more, I have to refresh the page again.

Is anyone able to identify the problem here? Please help.

NOTE: My table actually is a Shopping Cart which contains a product in each row. [X] button actually signifies "removing the item" from the cart.

Here is the JavaScript code:

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();
}

The function removeitem shows an alert "The item to be removed is: 123" for the first time but does not shows second time. When I am checking in Firebug console, the following error is coming second time:

removeitem is not a function

Please help!!

All my Javascript functions are in a separate file (sc.js) for which I am using <script type="text/javascript" src="js/sc.js"></script> in the HEAD tag of my page.

My viewpoint: So finally I think the question comes to a simple point: If a webpage is requesting some HTML from a PHP page using XMLHTTPRequest -- and if that HTML (sent by PHP) contains some button which is calling a Javascript function -- then what will happen in this situation[?]. Now since I am able to remove the row for the first time, I think it is false to say that above situation will not work. It is just that the code that came from PHP and the Javascript which has already been loaded are not aware of each others presence when I click the button second time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

药祭#氼 2024-12-25 02:13:08

removeitem 函数中,您将通过 XMLHttpRequest 对象覆盖 removeitem。因此,后面的错误removeitem is not a function

要解决此问题,请在 removeitem 前面加上 var (推荐),或使用其他变量名称,或两者兼而有之。

推荐代码:

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", linktoexecute, 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");
}

Inside the removeitem function, you're overwriting removeitem by a XMLHttpRequest object. Hence the later error removeitem is not a function.

To fix this, either prefix removeitem by var (recommended), or use another variable name, or both.

Recommended code:

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", linktoexecute, true);
    removeitem.send();
}

Adding var before removeitem fixes the problem, because the variable is locally defined. When var is omitted, a new value will be attached to the closest parent declaration (the function, in this case).

var removeitem = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");

The previous line is short for:

var removeitem;
if (window.XMLHttpRequest) {
    removeitem = new XMLHttpRequest();
} else {
    removeitem = new ActiveXObject("Microsoft.XMLHTTP");
}
心作怪 2024-12-25 02:13:08

看起来你并没有像 @Rob W 所说的那样转义双引号。我在使用 php 写出 HTML 时通常使用单引号。我认为这是一个很好的做法。

我唯一能想到的另一件事是,也许您的函数不在 AJAX 响应文本之外。只需检查 Firebug 或查看第一个响应返回后呈现的内容的源代码即可。

我希望这有帮助。

it doesn't look like you're escaping your double quotes like @Rob W has stated. I generally use single quotes when using php to write out HTML. I find this to be a good practice in my opinion.

The only other thing I can think of is that maybe your function isn't outside AJAX response text. Just check Firebug or view source of what's being rendered after the first response returned.

I hope this helps.

著墨染雨君画夕 2024-12-25 02:13:08

为什么每次点击都要加载所有表格 html?删除时以及使用 jQuery 删除 TR 元素后,您只能发送 true 或 false。

删除TR:
在您的 removeitem 函数中,在 ajax 响应后,只需调用 $(this).parents("tr").remove();

Why you load all table html every click? You can send only true or false when deleting and after you can remove TR element with jQuery.

Removing TR:
In your removeitem function, after ajax response, just call $(this).parents("tr").remove();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文