如何使用Javascript发出警报

How to make an alert with Javascript

本文关键字:何使用 Javascript      更新时间:2023-09-26

我有一个带有几个文本框的.NET应用程序。如果用户按下提交按钮时表单不完整,我需要弹出一个框(我认为是"警报"),上面写着"此表单必须由员工填写"。关闭弹出窗口后,它需要重定向到下一个表单。我认为这应该很简单,但我对javascript和客户端功能知之甚少。或者有没有一种方法可以在服务器端写这篇文章?提前感谢!

是的,有ClientScriptManager.RegisterStartupScript:

http://msdn.microsoft.com/es-es/library/z9h4dk8y.aspx

但您最好使用Asp.Net:中已经提供的可用验证器

http://www.tutorialspoint.com/asp.net/asp.net_validators.htm

您可以在javascript函数中放入if语句。

alert("This form must be completed by the employee");
//automatic link to new page
window.location.href = "http://www.google.com";

希望这能帮助

在html代码中,您将拥有按钮:

 <body>
   <input type="button" Value="Click Me" onclick="myAlertFunction()" />
 </body>

<head></head>中,您将编写这样的脚本:

<head>
  <title></title>
  <script type="javascript/text">
     function myAlertFunction()
     {
        alert("This form must be completed by the employee");
     }
  </script>
</head>

祝你好运!:)

如果您想在客户端验证表单中的数据,可以使用javascript在每个文本框中获取值并进行一些验证。如果有些错误,你可以使用提醒功能发送一些消息来使用,比如

alert("This form must be completed by the employee");

如果您不熟悉JS,可以在服务器端进行验证。您可以通过以下C#代码向客户端发送警报

Page.ClientScript.RegisterStartupScript(GetType(), "ALERT","message to alert", true);
Java脚本函数
function ShowError() {
      alert("Please enter all the fields");
      return false;
  }

在代码隐藏(C#)中调用函数

 ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "ShowError();", true);