可以't从表行上的按钮获取引导程序按钮ID

Can't get bootstrap button ID from button on a table row

本文关键字:按钮 获取 ID 引导程序 可以      更新时间:2023-09-26

我正在使用Bootstrap,我认为我会很聪明,把一些按钮放在桌子里。唉,什么都没发生。按钮状态发生变化,但除此之外,没有错误,没有警报,根本没有响应。

我的缩写表

<table class="table">
    <tr>
        <td>Entry of some sort</td>
        <td><button type="button" class="btn btn-default btn-xs" id="edit_Button">Edit</button></td>
        <td><button type="button" class="btn btn-default btn-xs" id="delete_Button">Delete</button></td>
    </tr>
</table>

jQuery for my button

$( document ).ready(function() {    
    $(".table .btn").click(function(e){
        e.preventDefault();
        var id = $(this).attr('id');
        alert(id);
    });
});

$(document).ready(function () 中定义函数

由于您的按钮是在DOM中动态创建的,因此单击事件将不适用于这些按钮。在这种情况下,事件委派将帮助您附加该事件。

试试这个

$(document).ready(function () {
    $(document).on('click', ".table .btn", function (e) {
        e.preventDefault();
        id = $(this).attr('id')
        alert(id);
    });
});

演示

请在Dom Ready 上添加您的功能

试试这个

    $(function(){
        $('body').on('click','.table .btn','',function(e){
          e.preventDefault();
          var id = $(this).attr('id');
          alert(id);
         });
     });

在此处检查演示

在中编写jquery

$(document).ready(function(){
 //here
      $(".table .btn").click(function(e){
      e.preventDefault();
      id = $(this).attr('id')
      alert(id);
      });
});