当用户在警报框上单击“确定”时运行另一个 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10715244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:47:55  来源:igfitidea点击:

Run another javascript when user clicks "ok" on alertbox

javascriptjqueryfunctionalert

提问by user1323294

Basically I have a script that checks database every 10 seconds and notifys user if data has changed with a javascript alert box. But I need the database also to be changed when user has seen the alert and clicked OK. So is it possible to make a javascript function run when user clicks "OK" on javascript alert?

基本上我有一个脚本,它每 10 秒检查一次数据库,并通过 javascript 警报框通知用户数据是否已更改。但是当用户看到警报并单击“确定”时,我还需要更改数据库。那么当用户在 javascript 警报上单击“确定”时,是否可以运行 javascript 函数?

So for example

所以例如

<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("New data!");
}
</script>
</head>
<body>

<input type="button" onclick="show_alert()" value="Show alert box" />

</body>
</html>

And when user clicks OK it should run this function

当用户点击确定时,它应该运行这个功能

  function UpdateDB()
  {
      jQuery.ajax({
       type: "POST",
       url: "update.php",
       data: 'condition=ok',
       cache: false,

     });
 }

回答by Jamie Dixon

The alertfunction halts execution of the code until it's dismissed. This means that any code you want to run after the alert has been clickd can simply be placed after the call to the alertmethod.

alert函数会暂停代码的执行,直到它被解除。这意味着您想要在点击警报后运行的任何代码都可以简单地放置在调用该alert方法之后。

alert("New data!");
UpdateDB();

回答by Yevgeny Simkin

What you're really looking for is the confirm(). I don't think you can act on an alert's ok press (and really you don't want to since it doesn't leave the user the option to change their mind!)...

您真正要寻找的是confirm(). 我不认为你可以对警报的 ok 按下采取行动(而且你真的不想这样做,因为它不会让用户改变主意!)...

So...

所以...

if (confirm("Do you REALLY want this?")){
   //your AJAX CALL HERE
}

回答by ThiefMaster

function show_alert()
{
    alert("New data!");
    UpdateDB();
}