我如何实现一个确认对话框/ ajax请求之前发送到服务器

How do i implement a confirmation dialog/box before ajax request is send to server?

本文关键字:请求 ajax 服务器 确认 何实现 实现 一个 对话框      更新时间:2023-09-26

在我的管理面板中,我使用Jquery/Ajax通过点击链接编辑用户详细信息。这是成功的工作,但我想显示确认对话框/弹出框之前的请求被发送到服务器。如果它是yes,那么我的函数(myfunc1)将是执行的,否则它将关闭。我现在的代码是这样的

<td width="70"><a href="#" onClick="myfunc1('<?php echo $uid;?>') "><input type="button" value="Edit Details" class="button" /></a></td>
function myfunc1(id) {
    id = id;
    $.ajax({
        url: 'edit_user_details.php',
        type: 'post',
        data: {
            'id': id
        },
        success: function (response) {
            //$('#edit_user_result').html(response);          
            $('#sent_password_result' + id).html(response);
            setTimeout(function () {
                $('input[type=submit]').attr('disabled', false);
                window.location.href = "users.php";
            }, 5000);
        }
    });
}

我如何用jquery/Javascript做到这一点?谢谢你的帮助。

可以调用confirm()

function myfunc1(id) {
    if (confirm("OK to submit?")) {
        $.ajax({        
            url: 'edit_user_details.php',
            type: 'post',             
            data: {'id' : id},
            success: function(response) {
            //$('#edit_user_result').html(response);          
                $('#sent_password_result'+id).html(response);
                setTimeout(function() {
                    $('input[type=submit]').attr('disabled', false); 
                    window.location.href = "users.php"; 
                }, 5000 );  
            }           
        });
    }
}

或者你可以在onclick中调用它:

onclick="if (confirm('OK to submit?')) { myfunc1('<?php echo $uid;?>'); } "