简体   繁体   English

JavaScript 单击确认时确认框不起作用

[英]JavaScript Confirm Box not working when click confirm

I try to make a confirm box to inform user if they really want to show password field in table.如果他们真的想在表格中显示密码字段,我会尝试制作一个确认框来通知用户。 But it not working when click Confirm.但是当点击确认时它不起作用。

Here's my code:这是我的代码:

HTML HTML

<span onclick="myFunction()" class="fa fa-fw fa-eye fa-eye-slash field-icon toggle-password"></span>
<span style="display:none;" id="password-field">123456</span>

JS: JS:

function myFunction() {
          
          if (confirm("Are you sure you want to show your password on the screen?)) {
              
              $(".toggle-password-<?=$model->id?>").click(function() {

            $(this).toggleClass("fa-eye-slash");
             
            });
                

            $(document).ready(function(){
                $(".toggle-password-<?=$model->id?>").click(function(){
                $("#password-field-<?=$model->id?>").toggle();
                });
            }); 
        
          } else {
            //do nothing
          }
          
        }

Please help me with this.请帮我解决一下这个。

Thank you!谢谢!

here's an example for fixed code: https://codepen.io/mitni455/pen/YzwJwNL?editors=1010这是固定代码的示例: https://codepen.io/mitni455/pen/YzwJwNL?editors=1010

I have removed the PHP specific stuff.我已经删除了 PHP 特定的东西。

 function myFunction() { if (confirm("Are you sure you want to show your password on the screen?")) { $('#password-field').show(); } else { $('#password-field').hide(); } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button onclick="myFunction()"> My Function </button> <span style="display:none;" id="password-field"> 123456 </span>

OK a few things here:好的,这里有几件事:

  • When you click confirm you are simply adding 2 more click handlers and a document onload handler (it's also a memory leak).当您单击确认时,您只是添加了另外 2 个单击处理程序和一个文档加载处理程序(它也是 memory 泄漏)。
  • The code $(".toggle-password-<?=$model->id?>").click adds another (redundant) click handler to the .toggle-password-123 button代码$(".toggle-password-<?=$model->id?>").click将另一个(冗余)单击处理程序添加到.toggle-password-123按钮
  • You never made any call to show/hide or change the display value for the span#password-field ... this is resolved in the refactored code above您从未调用过显示/隐藏或更改span#password-field的显示值...这在上面的重构代码中得到了解决
  • You missed the closing quotation in the confirm("Are you sure you want to show your password on the screen?")您错过了confirm("Are you sure you want to show your password on the screen?")
  • $(document).ready won't fire and is redundant because the document will have already loaded by the time you click the button. $(document).ready不会触发并且是多余的,因为在您单击按钮时文档已经加载。 So you can remove that.所以你可以删除它。

Here's what you did: on confirm:这是你所做的:确认:

  1. add a click handler to .toggle-password-{id} (which is the button you just clicked);将点击处理程序添加到.toggle-password-{id} (这是您刚刚点击的按钮);
  2. add a document onload event handler (to a document already loaded);添加文档 onload 事件处理程序(到已加载的文档);
  3. once the document has loaded (will never fire), add another click handler to .toggle-password-{id};文档加载后(永远不会触发),向.toggle-password-{id};
  4. Finally, when that is clicked THEN toggle the password field最后,当它被点击时,然后切换密码字段
<button onclick="myFunction()" class="fa fa-fw fa-eye fa-eye-slash field-icon toggle-password">Click me</button>
<span style="display:none;" id="password-field">123456</span>

<script type="text/javascript">

function toggledisplay(elementID)
     {
        (function(style) {
             style.display = style.display === 'none' ? '' : 'none';
                })(document.getElementById(elementID).style);
     }

function myFunction() {  
        
    if (confirm("Are you sure you want to show your password on the screen?")) {
        toggledisplay("password-field");
        
      }
}

I used the button tag to be able to see where to click, when replicating your code.在复制代码时,我使用了button标签来查看单击的位置。 Then, added the function toggledisplay(elementID) to toggle the display style whenever the confirm is True .然后,添加 function toggledisplay(elementID)以在confirmTrue时切换显示样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM