简体   繁体   English

<button>单击后始终重定向</button>

[英]<button> always redirecting after clicked

My button: 我的按钮:

<button onclick="return DeleteImg(this,event);">Delete</button>

My code: 我的代码:

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;
}

Post data function: 发布数据功能:

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)
}

Whenever the button is clicked and the user clicks okay to the confirmation box, the page redirects to another page (No idea what, my FCP just redirects it to page not found though so it doesn't exist) and the code isn't ran. 每当单击按钮并且用户单击确定到确认框时,页面就会重定向到另一个页面(不知道是什么,我的FCP只是将其重定向到未找到的页面,因此它不存在)并且代码不会运行。

What can I do to fix this? 我该怎么做才能解决此问题?

By default a button is a submit button which causes your page to redirect. 默认情况下,一个按钮是“ submit按钮,可导致您的页面重定向。 You can fix by setting the button type. 您可以通过设置按钮类型进行修复。

<button type="button" onclick="return DeleteImg(this,event);">Delete</button>

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

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