【问题标题】:jquery dialog widget not work with onclick button in row tablejquery 对话框小部件不适用于行表中的 onclick 按钮
【发布时间】:2015-10-17 12:16:27
【问题描述】:

我有如下图所示的表格,我在其中从查询创建到数据库,每一行都有我想要的按钮,当 onclick 将是提示对话框(我使用 jquery 对话框小部件)

这是我创建表格的 php 代码

while ($fetch_dbsi_mhsw=mysql_fetch_array($query_dbsi_mhsw)) {
$no++;
echo" <tr>
<td>$no</td>
<td>$fetch_dbsi_mhsw[NIM]</td>
<td>$fetch_dbsi_mhsw[Name]</td>
<td style=\"text-align: center;\"><input name=\"bt_tambah_calon_wisudawan\" id=\"bt_tambah_calon_wisudawan\" type=\"image\" src=\"buttonTambah.png\" alt=\"Tambah\" align=\"middle\" width=\"20\" height=\"20\"  /></td>  </tr>";}

还有我的 jquery 代码

$(document).ready(function(){
$("#bt_tambah_calon_wisudawan").click(function(){
    var value1 = $(this).closest('tr').find('td:eq(1)').text();
    var value2 = $(this).closest('tr').find('td:eq(2)').text();
    // Here's the text of the dialog box 
    var dialog = $("<div style='display: none'><p>Anda akan menambahkan "+value1 + " " + value2 + " sebagai calon wisudawan?</p></div>").appendTo("body");
    // This is the button on the form
    var form = $("#form_tambah_cl_wisudawan")
    // The form button was pressed - open the dialog
    $(dialog).dialog(
    {
            title: "Konvirmasi Penambahan Data",
            dialogClass: "no-close",
            width: 600,
            modal: true,
            buttons: {
                "Tambah": function () {
                    // This will invoke the form's action - putatively deleting the resources on the server
                    $(form).submit();
                    $(this).dialog("close");
                },
                "Cancel": function() {
                    // Don't invoke the action, just close the dialog
                    $(this).dialog("close");
                }
            }
        });
    return false;
});

});

我的问题是当我单击第一行中的加号按钮时,将出现对话框(如图 2 所示)

但是为什么当我单击另一行中的加号按钮时,对话框没有出现,页面只是重新加载并且我的表格消失了。每当我单击一个加号按钮时,我想提示对话框。有什么帮助吗?

【问题讨论】:

    标签: javascript php jquery html


    【解决方案1】:

    使用重复标识符时会出现问题,标识符必须是唯一的。你可以添加一个通用的CSS类然后可以使用类选择器$('.className')

    HTML(此处为简体)

    <input class="bt_tambah_calon_wisudawan" />
    

    脚本

    $(".bt_tambah_calon_wisudawan").click(function(){
        var tr = $(this).closest('tr'),
            value1 = tr.find('td:eq(1)').text(),
            value2 = tr.find('td:eq(2)').text();
    
        //rest of code
    });
    

    【讨论】:

    • 感谢您的回答,这解决了我的问题,但是我的重复标识符在哪里?对不起,我的英语不好。
    • @eniac05,您生成的 ID 是一个 while 循环,所以 id=\"bt_tambah_calon_wisudawan\" 会生成多次
    猜你喜欢
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多