我写了一个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";
}
答案 0 :(得分:9)
解决方案是使用beforeSend
ajax属性。
beforeSend之前是一个预请求回调函数 send.Returning在beforeSend函数中将取消 请求。
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";
}
});
答案 1 :(得分:0)
将你的ajax写成如下函数:
function save(){
// something in here
}
之后写一个确认功能,如果用户确认则调用save()函数
答案 2 :(得分:0)
也许这个例子就是你所需要的?
var r = confirm("Press a button!");
if (r == true) {
// Make your ajax call here
} else {
// He refused the confirmation
}
在ajax电话会议之前致电您的确认?
答案 3 :(得分:0)
您可以尝试将确认消息放入beforeSend
方法:http://api.jquery.com/jquery.ajax/
答案 4 :(得分:0)
visibility: hidden;
答案 5 :(得分:0)
使用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/
答案 6 :(得分:0)
我最近遇到了这个问题,所以这是我的答案,我使用的是 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>
...