asp:按钮和javascript弹出窗口

asp:button and javascript popup

本文关键字:窗口 javascript 按钮 asp      更新时间:2023-11-19

我想添加一个简单的确认对话框,当你点击一个执行回发的asp:按钮时,你确定吗。类似于:

OnClientClick="if(confirm('Format the hard disk?'))
alert('something');
else alert('something else')"

问题是,无论您是单击"确定"还是取消它,它仍然会返回。如何使它只在OK上发布?

如果您只是想根据确认对话框中的选择进行回发,这可能更简单:

OnClientClick="return confirm('Are you sure?');"

您还可以使用下一个:

OnClientClick="return confirm('Delete this entity?');"

简而言之,"....; return false;"停止点击,下面的例子不使用内联JS:

标记:

OnClientClick="confirmSomething()";

JS函数:

function confirmSomething()
{
    var result = confirm('Format the hard disk?');
    if(result == 1)
    {
       alert('something');
       return true; //continue click event
    }
    else if(result == 0)
    {
       alert('something else');
       return false; //stop click event
    }
}

您需要返回confirm。

请参阅:http://rumandcode.wordpress.com/2008/08/11/javascript-confirm-on-aspnet-button/

使用return false。

OnClientClick="if(confirm('Format the hard disk?')) alert('something'); else {alert('something else'); return false;}"