[英]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:好的,这里有几件事:
$(".toggle-password-<?=$model->id?>").click
adds another (redundant) click handler to the .toggle-password-123
button代码$(".toggle-password-<?=$model->id?>").click
将另一个(冗余)单击处理程序添加到.toggle-password-123
按钮span#password-field
... this is resolved in the refactored code above您从未调用过显示/隐藏或更改span#password-field
的显示值...这在上面的重构代码中得到了解决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:这是你所做的:确认:
.toggle-password-{id}
(which is the button you just clicked);将点击处理程序添加到.toggle-password-{id}
(这是您刚刚点击的按钮);.toggle-password-{id};
文档加载后(永远不会触发),向.toggle-password-{id};
<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)
以在confirm
为True
时切换显示样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.