【问题标题】:jquery: when an confirm is dynamically written can you return the true function?jquery:当动态编写确认时,你能返回真正的函数吗?
【发布时间】:2019-04-13 14:54:57
【问题描述】:

一个 php 页面有一个 jquery 接口。 提交按钮动态写入警报。 确认框的样式为:return confirm。

换句话说,页面上有一个脚本可以执行以下操作:

确认(好的取消);

我需要一种方法来监听警报何时被动态写入,以便我可以动态地确认它。 有什么想法吗?

是否有用于“确认”的事件侦听器?


    <html>
       <head>   
<script>           
                function getConfirmation() {
                   var retVal = confirm("Do you want to continue ?");
                   if( retVal == true ) {
                      document.write ("User wants to continue!");
                      return true;
                   } else {
                      document.write ("User does not want to continue!");
                      return false;
                   }
                }
           
          </script>     
       
       </head>
       
       <body>
          <p>Click the following button to see the result: </p>      
          <form>
             <input type = "button" value = "Click Me" onclick = "getConfirmation();" />
          </form>      
       </body>
    </html>

【问题讨论】:

  • 这是您触发警报的代码吗?更详细地解释您的用例。内置警报不会发出任何事件,但可能会根据用例处理您的情况
  • 这听起来真的很像XY Problem
  • @Davide Vitali :这个答案与 Selenium IDE 有关,现在无论如何都已弃用。所以不,不是重复的。
  • An alert()confirm() 不同 - 不,您不能通过 javascript 访问它们中的任何一个,因为警报/确认在它们显示时停止 javascript 线程并且 javascript 是单线程。
  • 请解释你想要发生的事情。您不希望用户看到或回答confirm 对话框吗?那你为什么首先拥有它?只是不要触发它......

标签: javascript jquery event-listener dynamic-html


【解决方案1】:

您可以使用window.confirm = function() { } 拦截confirm 命令。

返回true 与单击“确定”按钮相同。

window.confirm = function() { return true; }

if (window.confirm("Delete everything?") == true)
{
    alert("Deleting all your files as you confirmed")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    相关资源
    最近更新 更多