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

时间:2011-10-25 09:49:33

标签: php ajax 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名称只返回第一个项目名称,无论我点击第二个还是第三个等等.........我需要我要删除的名称。但是只显示警告第一.......

1 个答案:

答案 0 :(得分:0)

试试这个:

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

让我知道它是否有效。