简体   繁体   English

(AJAX) 如何制作 jQuery POST 删除确认按钮?

[英](AJAX) How do I make a jQuery POST delete confirmation button?

I have a ajax POST delete method with a jQuery modal popup which deletes a course just when I click the Delete button.我有一个带有 jQ​​uery 模式弹出窗口的 ajax POST删除方法,当我单击“删除”按钮时,它会删除一个课程。 The popup shows up, but when I close the popup and refresh the page, I see the record is deleted.弹出窗口显示出来,但是当我关闭弹出窗口并刷新页面时,我看到记录被删除。

Is there a way to implement a Confirmation button with jQuery popup, where it only gets deleted when the user clicks "Yes" and refreshes the page automatically?有没有办法用 jQuery 弹出窗口实现一个确认按钮,只有当用户单击“是”并自动刷新页面时它才会被删除?

Html html

@Html.ActionLink("Delete", "Delete", new { id = item.CourseID }, new { @class = "deletebtn", @id=item.DepartmentID })

<div id="dialog" title="Delete Department" style="display: none">
    <p>Are you sure you want to delete this Course?</p>
    <button id="confirm-deletion">Yes</button>
    <button id="abort-deletion">No</button>
</div>

Script脚本

    <script>
    $(function () {
        $(".deletebtn").on("click", function (e) {
            e.preventDefault();
            $('#dialog').dialog();

            var btnobj = $(this);
            var id = btnobj[0].id;

            $('#confirm-deletion').on("click", function() {

                //$('#dialog').dialog();

                //var btnobj = $(this);
                //var id = btnobj[0].id;
                //console.log(btnobj);

                var token = $('input[name="__RequestVerificationToken"]').val();
                var data = { id: id, __RequestVerificationToken: token };

                $.ajax({
                    type: "POST",
                    url: "@Url.Action("Delete","Course")",
                    data: data,
                    //ajaxasync: true,
                    success: function () {
                        console.log("success");
                    },
                    error: function () {
                        console.log("failed");
                    }
                });
            });
        });
    });
</script>

What gerrit said, define the click functions for each button in your dialog and move your code with ajax into a function. gerrit 所说的,为对话框中的每个按钮定义点击函数,并将带有 ajax 的代码移动到一个函数中。 Call the function in the click.在点击中调用函数。

 $( ".selector" ).dialog({
  buttons: [
    {
      text: "Ok",
      icons: {
        primary: "ui-icon-heart"
      },
      click: function() {
        $( this ).dialog( "close" );
      }

      // Uncommenting the following line would hide the text,
      // resulting in the label being used as a tooltip
      //showText: false
    }
  ]
});

jquery dialog docs jquery 对话框文档

You open the dialog, but that does not keep the AJAX request from being executed.您打开对话框,但这不会阻止执行 AJAX 请求。 Why would it?为什么会呢? What you do is simply opening a dialog and then sending your AJAX request immediately.您所做的只是打开一个对话框,然后立即发送您的 AJAX 请求。 You need to execute the request when a button inside of the modal dialog is clicked, not when it is opened.您需要在单击模态对话框内的按钮时执行请求,而不是在打开时执行请求。

HTML: HTML:

<div id="dialog" title="Delete Department" style="display: none">
  <p>Are you sure you want to delete this Course?</p>
  <button id="confirm-deletion">Yes</button>
  <button id="abort-deletion">No</button>
</div>

Javascript: Javascript:

$('#confirm-deletion').click(function() {
  // Send request here
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM