dsfhd78978 2015-02-18 16:17
浏览 175
已采纳

如何使用$ .ajax将变量传递给alert()?

I want to let the user confirm to deletion of a record by letting them click on the button and the button sends the value Yes to a $_POST request so I can delete the record with PHP, instead it's showing me the javascript file that contains the function.

What am I doing wrong, I've been tinkering and searching for 3 hours straight, while in the back of my mind I know it's a easy fix.

Any help is greatly appreciated.

If you think there is a smarter way to be doing this, please let me know.

$(function () {

    $("#DeleteConfirm").dialog({

        buttons: {
                 "Confirm": function() {
                    var DeleteConfirmation = "Yes";                 
                    $.ajax({
                        type: "POST",
                        url: "index.php",
                        data: DeleteConfirmation,
                        success: function(DeleteConfirmation){
                            alert(DeleteConfirmation);
                        },
                        dataType: "text"
                    });

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

});
  • 写回答

3条回答 默认 最新

  • duanqiao9541 2015-02-18 16:33
    关注

    Javascript:

    if( confirm('Are you sure?')){
        /*proceed?*/
    }
    

    That is standard javascript. It wraps your AJAX call within the braces and if the user clicks 'yes' - the ajax call will execute. If he click 'no', then nothing happens. The confirm() prompt pops up an alert on screen with buttons. The resulted is captured by the if. You can also capture the result of that: var res=confirm('are you sure?');.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题