将html按钮设置为回发并确认

时间:2013-08-20 11:49:33

标签: jquery .net

嗨,我有一个像这样的HTML按钮

<button type="button" runat="server" id="btnDelete" type="button" data-confirm-message="Deleting this page will permanently delete it from the system and it will not be recoverable. Are you sure you wish to continue?">delete</button>

哪个生成这个html

<button onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(''); __doPostBack('ctl00$body$btnDelete','')" id="btnDelete" type="button"   data-confirm-message="Deleting this page will permanently delete it from the system and it will not be recoverable. Are you sure you wish to continue?">delete</button>

和一些jQuery

$(document).ready(function () {

    $("#btnDelete").click(function () {
        return confirm($(this).data("confirm-message"));
    });

});

问题是页面被回发,无论确认是真还是假,任何帮助都会很棒

4 个答案:

答案 0 :(得分:0)

如果你分析你的内联处理程序,你会看到每次点击按钮时都会执行回发__doPostBack('ctl00$body$btnDelete','')

if (typeof(Page_ClientValidate) == 'function') 
    Page_ClientValidate(''); 

__doPostBack('ctl00$body$btnDelete','') // this line is always reached on click

如果你从jQuery点击处理程序返回false并不重要,因为无论如何都会执行内联代码。

答案 1 :(得分:0)

正如其他人所提到的,你有一个带有onclick属性的内联点击处理程序,它提交表单并且每次都会击败你的jQuery点击处理程序。

试试这个:

$(document).ready(function () {

    $("#btnDelete").removeAttr("onclick").click(function () {
        return confirm($(this).data("confirm-message"));
    });

});

它删除onclick属性,以便它可以处理点击本身。

答案 2 :(得分:0)

最后,我删除了click事件,并在确认为真后重新附加

 $(".document-confirm-delete").each(function (index, item) {
        var clickEvents = this.onclick;
        this.onclick = null;
        $(this).on("click", function () {
            if (Document.ConfirmDelete(this)) {
                clickEvents();
            }
        });
    });

感谢所有帮助

答案 3 :(得分:-1)

我看到你正在使用.NET。阻止回发的一种方法是将OnClientClick连接到ASP Button / LinkBut​​ton /等。

OnClientClick="return confirm('Your Message');"

false的响应会阻止回发,然后您可以在OnClick处理程序中包含回复代码以进行true响应。会有类似的工作吗?

相关问题