显示警报后调用方法/函数

call a method/Function after alert is shown

本文关键字:方法 函数 调用 显示      更新时间:2023-09-26

这是我打开警报的代码我想要的是,当我按下处于警报状态的"确定"时,我可以调用一个方法/函数。

ClientScriptManager CSM = Page.ClientScript;
string strScript = "<script>";
strScript += "alert('There is no Bookmarked Question Available');";
strScript += "  document.getElementById('btnReview').onclick=true";
strScript += "</script>";
ScriptManager.RegisterStartupScript(this, this.GetType(), "Startup", strScript, false);

alert停止代码的性能,因此一旦关闭alert将导致以下方法。如果需要确认,请使用 confirm

如果我

理解正确,您需要更改代码

,如下所示
ClientScriptManager CSM = Page.ClientScript;
    string strScript = "<script>";
    strScript += "alert('There is no Bookmarked Question Available');";
    strScript += "  document.getElementById('btnReview').onclick();";
    strScript += "</script>";
    ScriptManager.RegisterStartupScript(this, this.GetType(), "Startup", strScript, false);
在这种情况下,在

警报调用单击后,对于调用方法显示,如果是客户端方法,则如下所示

ClientScriptManager CSM = Page.ClientScript;
    string strScript = "<script>";
    strScript += "alert('There is no Bookmarked Question Available');";
    strScript += "  Display();";
    strScript += "</script>";
    ScriptManager.RegisterStartupScript(this, this.GetType(), "Startup", strScript, false);

我认为当用户按确定时无法执行window.alert

来自 Mozilla API Notes

警报对话框应用于不需要任何消息的消息 用户的响应,除了确认 消息。

对话框是模式窗口 - 它们阻止用户访问 程序界面的其余部分,直到对话框关闭。 因此,不应过度使用任何创建 对话框(或模式窗口)

此外,它不会给出任何结果,例如window.confirm gives true/false执行函数的任何callback method

试试这个:

string strScript = "<script>";
strScript += "  if (confirm('There is no Bookmarked Question Available')) { "
strScript += "  document.getElementById('btnReview').onclick=true";
strScript += " }"
strScript += "</script>";

创建一个名为"test"的jquery/javascript函数

function test(){
alert('There is no Bookmarked Question Available');
document.getElementById('btnReview').click();
}

现在只需像您正在做的那样从后面的代码中调用此方法:

ScriptManager.RegisterStartupScript(this, this.GetType(), "Startup","test();", true);

就这样