0

我的按钮:

<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 只是将其重定向到未找到的页面,因此它不存在)并且代码没有运行.

我能做些什么来解决这个问题?

4

1 回答 1

4

默认情况下,按钮是submit导致页面重定向的按钮。您可以通过设置按钮类型来修复。

<button type="button" onclick="return DeleteImg(this,event);">Delete</button>
于 2013-10-08T15:31:11.813 回答