删除确认对话框仅适用于表中的第一行

时间:2016-09-26 17:26:24

标签: javascript jquery dialog htmldatatable

这是我的表的示例,我在那里有2个图标用于编辑操作和删除。 删除时,我想创建一个弹出确认对话框。它仅在我单击第一行的删除图标时才有效。
对于其他行,缺少jQuery事件(不工作)。

enter image description here

这是我的.php编码文件。

<tbody>    
//data connection with db
while ($row = mysql_fetch_array ($res))
{
    //data rows
?>
<tr class="odd" role="row" id="row_5"> 
    <td align="center">
        <a href="#"><img src="../images/sys_icon_edit.png" width="20" height="20" alt="Edit"></a> 
        <a id="id-btn-dialog2" href="#"><img src="../images/icon_trash.png" width="20" height="20" alt="Delete"></a>
    </td>   
    //other data fields here
</tr>    
<?php
}
?>     
</tbody>

这是与之相关的jquery。

jQuery(function($) {

                $( "#id-btn-dialog2" ).on('click', function(e) {e.preventDefault();
                $( "#dialog-confirm" ).removeClass('hide').dialog({
                        resizable: false,
                        width: '320',
                        modal: true,
                        title_html: true,
                        buttons: [{
                                html: " Delete ",
                                "class" : "btn btn-danger btn-minier",
                                click: function() {
                                    $( this ).dialog( "close" );
                                }},
                                {
                                html: " Cancel ",
                                "class" : "btn btn-minier",
                                click: function() {
                                    $( this ).dialog( "close" );
                                }
                            }]
                    });
                });
})  

请帮帮我怎么办?我只是初学者级别的php,jQuery和javascript。
那个jQuery代码就是我在互联网上找到的,我刚刚更改了标题文本,而不是功能代码。

提前致谢。

2 个答案:

答案 0 :(得分:1)

您正在为元素的多个实例使用 ID /#

尝试使用类。

有关更多信息和研究,请访问this link

这是Example我将如何做到这一点。

答案 1 :(得分:1)

使用类作为删除按钮而不是id,并将处理程序设置为body标签,以便动态添加行支持。

$( "body" ).on('click', ".delete", function(e) {
            e.preventDefault();
            $( "#dialog-confirm" ).removeClass('hide').dialog({
                    resizable: false,
                    width: '320',
                    modal: true,
                    title_html: true,
                    buttons: [{
                            html: " Delete ",
                            "class" : "btn btn-danger btn-minier",
                            click: function() {
                                $( this ).dialog( "close" );
                            }},
                            {
                            html: " Cancel ",
                            "class" : "btn btn-minier",
                            click: function() {
                                $( this ).dialog( "close" );
                            }
                        }]
                });
            });