繁体   English   中英

在发送ajax请求之前显示确认消息

[英]Display confirmation message before send ajax request

我已经编写了一个ajax函数,我想在提交表单之前显示确认信息。 我应该如何添加我的条件。 下面是我的代码。

$.ajax({
                        url: "UBRDashboard.aspx/GetDllValue",
                        dataType: "json",
                        type: "POST",
                        contentType: 'application/json; charset=utf-8',
                        data: JSON.stringify({ ddlOduModel: ddlOduModel, ddlAntModel: ddlAntModel, ddlOMTModel: ddlOMTModel, ddlSapID: ddlSapID, ddlVendorName: ddlVendorName, strReqID: r.d, ddlSapDescVal: ddlSapDescVal, SITE_ADD: SITE_ADD, LATITUDE: LATITUDE, LONGITUDE: LONGITUDE, ddlEQP_SEQ: ddlEQP_SEQ, txtLinkID: txtLinkID, RJ_QUANTITY: RJ_QUANTITY, USER_NAME: USER_NAME, CREATED_DATE: CREATED_DATE, LOCATIONTYPE: LOCATIONTYPE, TOWERTYPE: TOWERTYPE }),
                        async: true,
                        processData: false,
                        cache: false,
                        success: function (r) {
                            if (r.d == "OK") {
                                alert('Record Saved successfully');
                                window.location.href = "UBRDashboard.aspx";
                            }
                        },
                        error: function (xhr) {
                            alert('Error while selecting list..!!');
                            window.location.href = "ErrorPage.aspx";
                        }
                    })
                },
                error: function (xhr) {
                    alert('Error while selecting list..!!');
                    window.location.href = "ErrorPage.aspx";
                }

解决方案是使用beforeSend ajax 属性。

beforeSend 是发送之前的预请求回调函数。beforeSend 函数中返回false 将取消请求。

beforeSend:function(){
     return confirm("Are you sure?");
},

AJAX

$.ajax({
      url: "UBRDashboard.aspx/GetDllValue",
      dataType: "json",
      type: "POST",
      contentType: 'application/json; charset=utf-8',
      data: JSON.stringify({ ddlOduModel: ddlOduModel, ddlAntModel: ddlAntModel, ddlOMTModel: ddlOMTModel, ddlSapID: ddlSapID, ddlVendorName: ddlVendorName, strReqID: r.d, ddlSapDescVal: ddlSapDescVal, SITE_ADD: SITE_ADD, LATITUDE: LATITUDE, LONGITUDE: LONGITUDE, ddlEQP_SEQ: ddlEQP_SEQ, txtLinkID: txtLinkID, RJ_QUANTITY: RJ_QUANTITY, USER_NAME: USER_NAME, CREATED_DATE: CREATED_DATE, LOCATIONTYPE: LOCATIONTYPE, TOWERTYPE: TOWERTYPE }),
      async: true,
      processData: false,
      cache: false,
      beforeSend:function(){
         return confirm("Are you sure?");
      },
      success: function (r) {
        if (r.d == "OK") {
        alert('Record Saved successfully');
        window.location.href = "UBRDashboard.aspx";
      },
      error: function (xhr) {
             alert('Error while selecting list..!!');
             window.location.href = "ErrorPage.aspx";
      }
});

使用 ajax beforeSend 回调函数。

beforeSend: function () {
                    if(confirm("Are you sure?")){
                        // do something
                    } else { 
                        // stop the ajax call
                        return false;
                    }
                },

请参阅文档 Ajax http://api.jquery.com/jquery.ajax/

将您的 ajax 写入如下函数:

function save(){
   // something in here 
}

之后写一个确认功能,如果用户确认然后调用 save() 函数

也许这个例子是你需要的?

var r = confirm("Press a button!");
if (r == true) {
    // Make your ajax call here
} else {
    // He refused the confirmation
}

在 ajax 调用之前打电话确认?

您可以尝试将确认消息放在beforeSend方法中: http : //api.jquery.com/jquery.ajax/

if ( confirm("Do you want to Submit?")) {
    // If you pressed OK!";

 $.ajax({
  url: "UBRDashboard.aspx/GetDllValue",
  dataType: "json",
  type: "POST",
  contentType: 'application/json; charset=utf-8',
  data: JSON.stringify({ ddlOduModel: ddlOduModel, ddlAntModel: ddlAntModel, ddlOMTModel: ddlOMTModel, ddlSapID: ddlSapID, ddlVendorName: ddlVendorName, strReqID: r.d, ddlSapDescVal: ddlSapDescVal, SITE_ADD: SITE_ADD, LATITUDE: LATITUDE, LONGITUDE: LONGITUDE, ddlEQP_SEQ: ddlEQP_SEQ, txtLinkID: txtLinkID, RJ_QUANTITY: RJ_QUANTITY, USER_NAME: USER_NAME, CREATED_DATE: CREATED_DATE, LOCATIONTYPE: LOCATIONTYPE, TOWERTYPE: TOWERTYPE }),
  async: true,
  processData: false,
  cache: false,
  beforeSend:function(){
     return confirm("Are you sure?");
  },
  success: function (r) {
    if (r.d == "OK") {
    alert('Record Saved successfully');
    window.location.href = "UBRDashboard.aspx";
  },
  error: function (xhr) {
         alert('Error while selecting list..!!');
         window.location.href = "ErrorPage.aspx";
  }
 });

} else {
    // If you pressed Cancel!";

}

请与window.confirm核对

我最近遇到了这个问题,所以这是我的答案,我使用 jquery 和 jqueryconfirm,“beforesend”callbak 只允许标准的“alert”和“confirm”功能。

我所做的是放置一个“假”提交按钮并隐藏实际提交按钮,因此很容易处理来自自定义确认对话框的响应,一旦我从对话框中得到肯定的答案,我就会调用隐藏的提交按钮。

...
<button id="confirmSave" type="button">Save</button>
<button id="save" class="is-hidden" type="submit"></button>
<button id="close" aria-label="close" type="reset">Cancel</button>
...

暂无
暂无

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

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