无法从表格行上的按钮获取引导按钮ID

时间:2014-04-11 06:19:20

标签: javascript jquery button twitter-bootstrap-3

我正在使用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);
    });
});

3 个答案:

答案 0 :(得分:3)

$(document).ready(function ()

中定义函数

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

试试这个

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

DEMO

答案 1 :(得分:1)

请在Dom Ready

上添加您的功能

试试这个

    $(function(){

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

     });

<强> CHECK DEMO HERE

答案 2 :(得分:0)

中编写你的jquery
$(document).ready(function(){
 //here
      $(".table .btn").click(function(e){
      e.preventDefault();
      id = $(this).attr('id')
      alert(id);
      });
});
相关问题