从 smarty 中检索值并在 ajax 中显示

发布于 2024-12-11 12:19:03 字数 1564 浏览 0 评论 0原文

我有以下 smarty 代码要在表中显示......

<table id="hor-zebra" summary="DIsh Information">
        <thead>
        <tr>
            <th scope="col">Item Name</th>
            <th scope="col">Item Price</th>
            <th scope="col">Item Type</th>
            <th scope="col">Quantity</th>
        </tr>
    </thead>

{foreach name=f1
         item=k
         from=$res}

         <tbody><tr class="odd">
            <td id="bn"> {$k->b_name}</td>
            <td> {$k->b_price}</td>
            <td> {$k->b_type}</td>
            <td> {$k->b_quantity}</td></tr>
             <tr><td><a href="javascript:remove()">Remove</a></td>
                 <td id="resId"></td></tr>


        {/foreach}
        </tbody></table>

而 ajax 代码是......

    function remove(){
   var http = GetXmlHttpObject();
    http.onreadystatechange = function()
    {
        if(http.readyState==4)
        {
            document.getElementById("resId").innerHTML = http.responseText;
            //alert(http.responseText);
        }
    }
    var name = document.getElementById("bn").innerHTML;
    alert(name);

    var url = "index.php?menu=rmv&ajax=ajax&q="+name;
    http.open('POST',url,true);
    http.send(null);
}

现在的问题是 var name 只返回第一个项目名称,无论我单击第二个还是第三个等等...... ......我需要我想删除的名称。但警报仅显示第一个......

I have the following smarty code to display in table....

<table id="hor-zebra" summary="DIsh Information">
        <thead>
        <tr>
            <th scope="col">Item Name</th>
            <th scope="col">Item Price</th>
            <th scope="col">Item Type</th>
            <th scope="col">Quantity</th>
        </tr>
    </thead>

{foreach name=f1
         item=k
         from=$res}

         <tbody><tr class="odd">
            <td id="bn"> {$k->b_name}</td>
            <td> {$k->b_price}</td>
            <td> {$k->b_type}</td>
            <td> {$k->b_quantity}</td></tr>
             <tr><td><a href="javascript:remove()">Remove</a></td>
                 <td id="resId"></td></tr>


        {/foreach}
        </tbody></table>

And the ajax code is....

    function remove(){
   var http = GetXmlHttpObject();
    http.onreadystatechange = function()
    {
        if(http.readyState==4)
        {
            document.getElementById("resId").innerHTML = http.responseText;
            //alert(http.responseText);
        }
    }
    var name = document.getElementById("bn").innerHTML;
    alert(name);

    var url = "index.php?menu=rmv&ajax=ajax&q="+name;
    http.open('POST',url,true);
    http.send(null);
}

Now the problem is that var name is returning only the first Item Name whether i click 2nd or 3rd or so on.........I need the name which i want to remove.But alert showing only the 1st.......

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

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

发布评论

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

评论(1

影子是时光的心 2024-12-18 12:19:03

试试这个:

<table id="hor-zebra" summary="DIsh Information">
        <thead>
        <tr>
            <th scope="col">Item Name</th>
            <th scope="col">Item Price</th>
            <th scope="col">Item Type</th>
            <th scope="col">Quantity</th>
        </tr>
    </thead>

{foreach name=f1 item=k key = ky from=$res}

         <tbody><tr class="odd">
            <td id="bn{$ky}"> {$k->b_name}</td>
            <td> {$k->b_price}</td>
            <td> {$k->b_type}</td>
            <td> {$k->b_quantity}</td></tr>
             <tr><td><a href="javascript:remove('{$ky}')">Remove</a></td>
                 <td id="resId{$ky}"></td></tr>


        {/foreach}
        </tbody>
</table>

并将你的 javascript 更改为:

function remove(key){
       var http = GetXmlHttpObject();
        http.onreadystatechange = function()
        {
            if(http.readyState==4)
            {
                document.getElementById("resId"+key).innerHTML = http.responseText;
                //alert(http.responseText);
            }
        }
        var name = document.getElementById("bn"+key).innerHTML;
        alert(name);

        var url = "index.php?menu=rmv&ajax=ajax&q="+name;
        http.open('POST',url,true);
        http.send(null);
    }

让我知道它是否有效。

Try this :

<table id="hor-zebra" summary="DIsh Information">
        <thead>
        <tr>
            <th scope="col">Item Name</th>
            <th scope="col">Item Price</th>
            <th scope="col">Item Type</th>
            <th scope="col">Quantity</th>
        </tr>
    </thead>

{foreach name=f1 item=k key = ky from=$res}

         <tbody><tr class="odd">
            <td id="bn{$ky}"> {$k->b_name}</td>
            <td> {$k->b_price}</td>
            <td> {$k->b_type}</td>
            <td> {$k->b_quantity}</td></tr>
             <tr><td><a href="javascript:remove('{$ky}')">Remove</a></td>
                 <td id="resId{$ky}"></td></tr>


        {/foreach}
        </tbody>
</table>

And change you javascript to :

function remove(key){
       var http = GetXmlHttpObject();
        http.onreadystatechange = function()
        {
            if(http.readyState==4)
            {
                document.getElementById("resId"+key).innerHTML = http.responseText;
                //alert(http.responseText);
            }
        }
        var name = document.getElementById("bn"+key).innerHTML;
        alert(name);

        var url = "index.php?menu=rmv&ajax=ajax&q="+name;
        http.open('POST',url,true);
        http.send(null);
    }

Let me know if it worked or not .

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