【问题标题】:jQuery dialog box with dynamic table delete record button not working带有动态表删除记录按钮的jQuery对话框不起作用
【发布时间】:2012-07-18 05:35:13
【问题描述】:

我一直在编写一个脚本来打开一个对话框,在对话框中有一个动态表,其中包含从我的一个数据库中提取的数据。我在所有记录旁边放置了删除按钮,以便工作人员可以删除不必要的记录。
我试图使用触发 Ajax 请求的单击功能,然后立即从数据库中删除记录。
但是发生的情况是,当我单击删除按钮时,它会关闭对话框并且根本不会删除任何内容。如果有任何帮助,我已经复制了下面的代码。

非常感谢所有帮助:)

 //including all jquery library

<script>
    $(function() {
        $( "#dialog" ).dialog({
            autoOpen: false,
                    buttons: {

                "Add to Log": function() {
                $.ajax({
                    type: 'POST',
                    url: "check_add.php",
                    data: $('#checkup').serialize(),

                    success: function(data) {

                             if(data == 0){//Failure 
                                    alert('Data was NOT saved in db!');
                                         }
                                 }
                });
                  $( this ).dialog( "close" );


                },
                Exit: function() {
                    $( this ).dialog( "close" );
                }
            }

        });

            $( "#opener" ).click(function() {
            $( "#dialog" ).dialog( "open" );
            return false;
        });


        $( "#reminder" ).dialog({
            width: 471,
            autoOpen: false,
                    buttons: {

                Exit: function() {
                    $( this ).dialog( "close" );
                }
            }
        });

            $( "#remind" ).click(function() {
            $( "#reminder" ).dialog( "open" );
            return false;
        });


        $(".Delete").click(function() // here the delete function
        {
            var $this = $(this);
            var rowid = $this.attr('name');
            $.ajax({
            type: "POST",
            url: 'check_del.php',
            data: {
            'id': rowid
                },
            success: function(data) {

        if(data == 1){//Success 
             alert('Sucess');
          }
        if(data == 0){//Failure 
             alert('Data was NOT saved in db!');
          }
            }
 });
        }); 


    });
    </script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<button id="opener">Add Damage</button>
<button id="remind">Reminder</button>

<div id="dialog" style="font-size:small;">
<form name="checkup" method="post" id="checkup"> 
    <table>
    <tr><td>
    <strong>Cases:</strong></td><td><input name="cases" type=text  placeholder="Cases ID" maxlength="7" style="width:129px;"></td>
    </tr>
    <tr>
    <td><strong>Comments:</strong></td>
    <td> <textarea name="comment" placeholder="Comments Box"></textarea></td>
    </tr>
    </table>
  </form>
</div>

<?php

//数据库连接的东西

$sql="select * from $tbl_name";
$result=mysql_query($sql);

?>


<div id="reminder" style="font-size:small;">
<form name="remind" method="post" id="remind"> 
    <table bordercolor="#000000" border="1" style="border:thin;">
    <th>Class</th>
    <th>Cases</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Model</th>
    <th>Comments</th>
    <th>   </th>

<?
    while($rows=mysql_fetch_array($result)){
$cases=strtoupper($rows['cases']);
?>

    <tr>
    <td><? echo $rows['hg']; ?></td>
    <td><? echo $cases;?></td>
    <td><? echo $rows['firstname']; ?></td>
    <td><? echo $rows['surname']; ?></td>
    <td><? echo $rows['model']; ?></td>
    <td><? echo $rows['comments']; ?></td>
    <td><button  name="<?php echo $rows['id']; ?>"  class="Delete">Delete</button></td>
    </tr>

    </table> 
<?
}
?>
    </form>
</div>

【问题讨论】:

  • 您是否尝试过在 chrome 中查看 ajax 流量(右键单击页面任意位置,检查元素,单击“网络”选项卡)或 firefox(使用 firebug...同时选择网络选项卡)?
  • 是的,我在 firefox 中使用 firebug 观看了流量,但它确实显示了一秒钟,但在我获得屏幕截图甚至阅读之前它就消失了。

标签: php ajax jquery dialog


【解决方案1】:

经过大量测试后,我稍微更改了代码。我已经为按钮和 jquery 代码粘贴了 html 的工作部分

HTML 按钮

<input name="<? echo $rows['id'];?>" type="button"   value="Delete"  />

jquery函数

$("input[type='button']").on('click', function() {

    var $this = $(this);
    var userid = $this.attr('name');

            $.ajax({
            type: "GET",
            url: 'check_del.php',
            data: {
            'id': userid

                },
            success: function(data) {

        if(data == 1){//Success 
             alert('Sucess');
          }
        if(data == 0){//Failure 
             alert('Data was NOT saved in db!');
          }
            }
    });
  });

【讨论】:

    【解决方案2】:

    试试这个

    $(".Delete").live('click',function() {
     // your code go here
    });
    

    【讨论】:

    • live 已弃用 - 尝试在 jQuery 1.7 或更高版本中使用 on,并在旧版本中选择 delegate 而不是 live
    • 对不起,凯莉,我不太明白你的意思?
    • 我试过用。 “on”而不是“live”,但它没有奏效我仍然面临同样的问题
    猜你喜欢
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    相关资源
    最近更新 更多