dongzhun1857 2013-07-23 15:07
浏览 54
已采纳

选择正确的变量php / javascript

I have a wishlist I built. It functions correctly with adding/displaying/remove items, but I want to take it further. When I delete an item, I want it to have a pop-up that says something like "Item X has been removed."

It's built inside an HTML table that generates the variable from a foreach.

My Problem: After it successfully deletes any item, the JS prompt will only grab the value of the first listed item.

I.E. If I click delete for any item, it will echo the variable from the first item everytime. It does however delete the correct everytime.

HTML

`echo '<tr >
                    <td >
                        '.$shirt_name.'
                    </td>
                    <td>
                        '.$model_number.'
                    </td> 
                    <td style="padding-left:9px;"><div style="height:50px;background-size:contain;" class="'.$shirt_image.'">
                       </div>
                    </td>

        ';              
         ?>
              <? echo '<td>  
              <form action="delete_button.php" method="post">
              <input type="hidden" id="deleteitem" class="testbtn" name="deleteitem" value="'.$delete_shirt. '">
              <input type="submit" id="del_but" value="Remove" class="deleteBtn">
              </form></td>                 
              ';

Javascript For brevity's sake here I'm only trying to echo the value of the input with
class="testbtn"

  <script>
$('.deleteBtn').click(function() {    
var deleteitem = $(".testbtn").val();
alert (deleteitem);
});
</script>`

So, it's only prompting the the value of the first listed <tr> instead of the relative one that I click. How do I make it grab the relative value?? I hope I made this clear enough!

  • 写回答

5条回答 默认 最新

  • doutan1875 2013-07-23 15:23
    关注

    try this

    <script>
    $('.deleteBtn').click(function() {  
    var deleteitem = $(this).parent().find('.testbtn').val();
    alert (deleteitem);
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题