【问题标题】:Attendee List with confirmation delete button带有确认删除按钮的与会者列表
【发布时间】:2013-06-03 09:46:02
【问题描述】:

我有一个与会者列表,其中包含填写活动表格的记录。每个表行都附加了一个删除按钮,以便管理员可以从数据库中删除一条记录。当单击删除按钮时,我尝试在表单中附加一个确认对话框(是/否)。

在我附加 javascript 之前,它可以很好地删除记录,但现在虽然对话框出现并且可以正常工作,但它并没有删除记录。这是我的代码:

<form id="attending" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <table class="attendees">
        <thead>
            <tr>
                <th class="name">Name</th>
                <th class="company">Company</th>
                <th class="email">Email</th>
                <th class="contact">Contact</th>
                <th class="day">Day</th>
                <th class="time">Time</th>
                <th class="delete">&nbsp;</th>
            </tr>
        </thead>
        <?php while ($row = mysql_fetch_assoc($result)) { ?>
            <tr>
                <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
                <td class="name"><?php echo $row['name']; ?></td>
                <td class="company"><?php echo $row['company']; ?></td>
                <td class="email"><?php echo $row['email']; ?></td>
                <td class="contact"><?php echo $row['contact']; ?></td>
                <td class="day"><?php echo $row['day']; ?></td>
                <td class="time"><?php echo $row['event-time']; ?></td>
                <td class="delete"><input type="submit" name="delete" class="delete-button" value="Delete"> </td>
            </tr>

    <?php if(isset($_POST['delete'])) { 
       $id = $_POST['id'];
       mysql_query("DELETE FROM `registered_blue` WHERE `id` = '$id' ");
       }
    ?>
  <?php } ?>
    </table>
</form>

下面是我的 jquery:

var confirmDelete = $('<div></div>')
        .html('Record will be deleted! This operation can\'t be undone. <br />Are you sure?')
        .dialog({
            autoOpen: false,
            title: 'Attendee will be deleted! Please Confirm.',
            buttons: {"Yes": function() { 
                $(this).dialog("close"); 
                $('#attending').submit();
            }, "No": function() {
            $(this).dialog("close");
         }
         }
    });  
    $('.delete-button').on('click', function(e){
        e.preventDefault();
        $(confirmDelete).dialog('open');
    });

谁能告诉我哪里出错了?谢谢

【问题讨论】:

  • 提交表单是否正确?
  • 现在我已经添加了我认为它没有提交的javascript,或者它是但没有从数据库中删除记录。
  • 您在循环中的表单元素上重用了相同的 name 属性。只会发布最后一行的值
  • 我认为
  • 如何命名每个删除按钮?

标签: php jquery


【解决方案1】:

当点击任何带有“删除按钮”类的东西时,以下脚本将弹出一个 JavaScript 对话框。如果用户单击“是”,应用程序将继续执行默认操作,如果用户单击“否”,应用程序将停止它应该执行的操作。

$('.delete-button').click(function () {
    return window.confirm("Attendee will be deleted! Please Confirm.');
});

您也可以使用原始脚本执行此操作,如果您希望它继续执行默认操作,您唯一需要的就是返回 true,如果您希望它停止,则返回 false。

编辑:我认为应该这样做:

$('.delete-button').on('click', function(e){
    $('<div></div>')
    .html('Record will be deleted! This operation can\'t be undone. <br />Are you sure?')
    .dialog({
        autoOpen: false,
        title: 'Attendee will be deleted! Please Confirm.',
        buttons: {"Yes": function() { 
            var result = true;
            $(this).dialog("close"); 
        }, "No": function() {
            var result = false;
            $(this).dialog("close");
        }
    }
    return result;
});

【讨论】:

  • 对不起,我有点迷茫,你能用这种方法修改我的脚本吗?
  • 这个有一些问题,第一次点击不删除记录,对话框不打开,只删除最后一条记录,无论按哪个删除按钮。
  • 我认为他的问题比我想象的要多,回到绘图板:(
  • 您应该使用带有 ID 的链接而不是按钮,因为将使用名称为“id”的最后一个输入,在这种情况下忽略其他输入。因此,只需为此使用链接。至于脚本,我又编辑了一遍,希望现在能用。
  • 当你说链接时,所以用锚标签替换输入,每个标签都有一个 ID?
猜你喜欢
  • 2023-03-24
  • 1970-01-01
  • 2017-09-26
  • 1970-01-01
  • 1970-01-01
  • 2015-03-14
  • 1970-01-01
  • 2022-08-14
  • 1970-01-01
相关资源
最近更新 更多