如何在动态创建的按钮上使用jQuery捕捉单击事件
我有一个HTML表,可以在其中动态创建新行。对于每一行,我还创建了一个按钮,可以删除刚刚创建的行。但是jQuery不会捕获动态创建的行中的事件。HTML代码:如何在动态创建的按钮上使用jQuery捕捉单击事件,jquery,html,html-table,row,Jquery,Html,Html Table,Row,我有一个HTML表,可以在其中动态创建新行。对于每一行,我还创建了一个按钮,可以删除刚刚创建的行。但是jQuery不会捕获动态创建的行中的事件。HTML代码: <table> ... <tbody id="nuevo_producto"> <tr id="1"> <td><input type = "hidden" name = "tabla-1">1</td> <td>&
<table>
...
<tbody id="nuevo_producto">
<tr id="1">
<td><input type = "hidden" name = "tabla-1">1</td>
<td><input readonly="readonly" class = "form-control" placeholder = "Ref." type = "text" size = "15" id = "modelo_ref_1" name = "modelo_ref_1" value="122"></td>
<td><input class = "form-control" placeholder = "Ref. C" type = "text" size = "15" id = "modelo_refc_1" name = "modelo_refc_1" value="231"></td>
<td><input class = "form-control" placeholder = "Modelo" type = "text" size = "60" id = "modelo_modelo_1" name = "modelo_modelo_1" value="sadsadsad"></td>
<td><input class = "form-control" placeholder = "PVP" type = "text" size = "15" id = "modelo_pvp_1" name = "modelo_pvp_1" value="12"></td>
<td><button class="btn btn-default delete_row" id="1" value="eliminar_fila"><span class="glyphicon glyphicon-minus"></span></button></td> <!-- Button to delete row -->
</tr>
<tr id="2">
... <!--another rows that I have created dinamically-->
</tr>
...
</tbody>
</table>
问题是“事件”按钮仅适用于现有行。如果创建新行,“单击”事件将不起作用。您需要使用
Id必须是唯一的
更改您的html
<tr id="1">
或 改变
<button class="btn btn-default delete_row" id="1"
您需要使用
Id必须是唯一的
更改您的html
<tr id="1">
或 改变
<button class="btn btn-default delete_row" id="1"
@IkerOcioZuazo Welocme很高兴它有帮助:)@IkerOcioZuazo Welocme很高兴它有帮助:)
$('#nuevo_producto').on('click','.delete_row',function(){
$('tr[data-id="'+$(this).data('id')+'"]').remove();
});
<button class="btn btn-default delete_row" id="1"
<button class="btn btn-default delete_row" data-id="1"
$('#nuevo_producto').on('click','.delete_row',function(){
$('#'+$(this).data('id')).remove();
});