jQuery:如何将其传递给单击按钮上的函数?

时间:2020-07-27 07:27:09

标签: html jquery

下面的代码可以正常工作,直到我通过jQuery插入新行。 “删除”按钮在新行中不起作用。

$(".delete").on("click", function() {
  $(this).closest('tr').remove();
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-striped">
  <tbody>
    <tr>
      <td> <input type="text" /></td>
      <td>
        <a href="#" class="delete btn btn-danger btn-xs" type="button">
          <i class="fa fa-trash"></i>
        </a>
      </td>
    </tr>
  </tbody>
</table>

1 个答案:

答案 0 :(得分:1)

如果动态添加新行,则必须像这样进行绑定;

$(document).on("click",".delete", function() {...

因为您需要绑定动态创建的元素。

相关问题