javascript onclick返回确认然后提交

javascript onclick返回确认然后提交,javascript,asp.net-mvc,Javascript,Asp.net Mvc,在使用javascript提交表单之前,我需要确认删除,我尝试了以下代码,但无效: <a href="#" onclick="return confirm('Confirm Delete'); $(this).closest('form').submit();"> Delete</a> 您应该在提交表单之前先确认。您需要设置一个条件,它是否返回true或false <a href="#" onclick="if(confirm('Confirm Delete'))

在使用javascript提交表单之前,我需要确认删除,我尝试了以下代码,但无效:

<a href="#" onclick="return confirm('Confirm Delete'); $(this).closest('form').submit();"> Delete</a>

您应该在提交表单之前先确认。您需要设置一个条件,它是否返回true或false

<a href="#" onclick="if(confirm('Confirm Delete')) $(this).closest('form').submit();"> Delete</a>

请对照代码检查注释

$(function () {
    //Attach click event to the link. In this case all links
    //You might want to update this, make it more specific by using id or name or class of the a tag

    $('a').on('click', function (event) {
       //prevent the default action of the tag
        event.preventDefault();
        //confirm
        var conf = confirm('Confirm Delete');
        if (conf) {
         //you action if true
        }
        else {
            return;
        }
    });
});

仅在用户确认后提交表单,否则设置为return false。所以你的表格不会被提交

function confirmdelete() {
    if (confirm("Are you sure?")) {
        // submit form
    }
    return false;
}

那么你是在确认之前提交表格的。也许是另一种方式?如果我在subimt之前添加confirm,它只确认并提交不起作用,但使用此代码它确认并提交,但它没有等待按ok:)