dsaf32131 2014-03-24 06:14
浏览 28
已采纳

获取具有动态ID值的元素的ID

I am a newbie in Jquery. I have a table in my webpage which shows the contents of a user table from the database.I have also created an edit button and Delete button for each row of data.I need the id of each edit and delete button for writing code for editing and deleting the records in the database as well as implementing this changes in the table in the webpage also.

   $query = mysqli_query($con,"SELECT * FROM user_details");
      while($row = mysqli_fetch_array($query))
      {
          echo "<tr>";
          echo "<td>".$row['fname']."</td>";
          echo "<td>".$row['mname']."</td>";
          echo "<td>".$row['childname']."</td>";
          echo "<td><input type='button' id = 'edit".$row['Id']."' value='Edit'></td>";
          echo "<td><input type='button' id = 'delete".$row['Id']."' value='Delete'></td>";
          echo "</tr>";
      }

If I am using Jquery,how to get the IDof each button?Or If I write a onclick event using javascript for each button and passes the ID's as arguments,can I access via Jquery.Please help me

  • 写回答

5条回答 默认 最新

  • doujiang1913 2014-03-24 06:22
    关注

    Add common class name and use that as a selector to get id of the buttons like below

    Try like this

    $(document).on('click','.edit_btn',function(){
      alert($(this).attr('id'));
    });
    $(document).on('click','.del_btn',function(){
      alert($(this).attr('id'));
    });
    

    HTML

    <input type='button' id ='edit' class="edit_btn" value='Edit'>
    <input type='button' id ='delete' class="del_btn" value='Delete'>
    

    DEMO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面