我的按钮:
<button onclick="return DeleteImg(this,event);">Delete</button>
我的代码:
function DeleteImg(Obj,e) {
if (!confirm('Are you sure you want to delete the file?')) return false;
var n = new PostData('/Pictures/Delete/', 'Type=Home&Name=' + window.LargeImage.src.split('/')[5]);
n.onResponse = function (t) {
var n = JSON.parse(t);
if (n.Success) ShowMsg('Image Deleted, it will be gone when you refresh.');
else ShowMsg(n.Msg);
};
e.preventDefault();
return false;
}
发布数据功能:
function PostData(e, t) {
var n = false;
this.onResponse = function (e) {};
if (window.XMLHttpRequest) n = new XMLHttpRequest;
else if (window.ActiveXObject) n = new ActiveXObject('Microsoft.XMLHTTP');
else {
this.onResponse(JSON.stringify({
Success: false,
Msg: 'Your browser does not support AJAX, please upgrade to Google Chrome, Mozzila Firefox or Internet Explorer 10',
ElementId: undefined
}))
}
n.open('POST', e, true);
n.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
var r = this;
n.onreadystatechange = function () {
if (n.readyState == 4 && n.status == 200) r.onResponse(n.responseText)
};
n.send(t)
}
每当点击按钮并且用户点击确认框时,该页面会重定向到另一个页面(不知道是什么,我的FCP只是将其重定向到找不到的页面,因此它不存在)并且代码不是跑了。
我该怎么做才能解决这个问题?
答案 0 :(得分:4)
默认情况下,按钮是submit
按钮,会导致您的网页重定向。您可以通过设置按钮类型来修复。
<button type="button" onclick="return DeleteImg(this,event);">Delete</button>