简体   繁体   English

在javascript中制作自定义确认框

[英]making a custom Confirm box in javascript

I want to make a custom made confirmation box in javascipt just like the built in confirm box. 我想在javascipt中创建一个自定义的确认框,就像内置的确认框一样。 the built in confirm box does not allow the code to progress unless the user selects atleast one thing. 内置的确认框不允许代码继续执行,除非用户选择了至少一件事情。 Below is my code: * ** * * HTML start * ** * * 下面是我的代码: * ** * * HTML开头 * ** * *

<div class = "popUp confirm" style="z-index:40000;"  id="confirmBlock">         
<div id = "confirmLabel" >Confirm Message</div>
<div style ="border:0px solid red;height:44.56px;">
<input id="Confirm" type="button" value="Confirm" onclick = "confirmAction(1)" />
<input id = "CancelConfirm" type="button" value="Cancel" onclick = "confirmAction(0)" />
</div>
</div>

** * ** HTML end * ** * * ** * ** HTML结束 * ** * *

** * ** Javascript start * ** * * ** * ** Javascript开始 * ** * *

var confirmresult = "-1";

function confirmationLoop()
{
    alert("If this alert is preesnt it works, seems like the built in alert provides some sort of pause for other parts of code to continue to work");

    if(confirmresult == "-1")
        confirmationLoop();     

    return; 
} 

function confirmAction(val)
{


    confirmresult = val;    
}

function checkuuu()
{
    confirmresult = "1";
}

function confirmMessage(message)                
{   
    document.getElementById("confirmLabel").innerHTML= message;
    //var check = setTimeout(function(){confirmAction(1)},5000);
    confirmationLoop(); 
    /*
    while(1)            //using while almost does not allow any other part to run at all hence tried recursion
    {
        if(confirmresult != "-1")
            break;  
    }
    */
    document.getElementById("confirmLabel").innerHTML= "Confirm Message";   
    var returnVal = confirmresult;
    confirmresult = -1;
    return returnVal;
}

** * ** Javascript end * ** * * ** * ** Javascript结尾 * ** * *

** * ** Sample code start * ** * * So this i what i expect below: ** * ** 示例代码开始 * ** * *所以这是我期望的以下内容:

function example
{
    var check = confirmMessage(message);
    //the next part of code should not execute untill i press confirm or cancel, using settimeout or settimeinterval is asynchronous and the code flow continues. i want the effect something like alert and confirm built in boxes         
}

** * ** Sample code end * ** * * ** * ** 示例代码结尾 * ** * *

I used loop but it keeps the thread completely occupied and does not give me a chance to press any button, which was quite obvious However recursion gives u the freedom to perform other activities. 我使用了循环,但是它使线程完全被占用,并且没有给我按下任何按钮的机会,这非常明显。但是,递归使您可以自由地执行其他活动。 The problem even though the value of confirmResult will become 1 upon pressing confirm button, which i check through alert. 即使确认结果的值将在按下确认按钮时变为1,但我仍会通过警报进行检查。 the recursive loop ie confirmation loop does not seem read it as 1. it still continues as -1. 递归循环(即确认循环)似乎未将其读取为1。它仍继续为-1。 If i put a alert in that confirmation loop the value wil be read as 1. Can anyone help me to achieve what i started out to?????? 如果我在该确认循环中发出警报,则该值将读为1。有人可以帮助我实现我刚开始的目标吗? Ps=> sorry for such a huge question!!! ps =>对这么大的问题感到抱歉!!!

You can't use any sort of loop - as you've found it'll just cause the browser to lock up. 您不能使用任何形式的循环-因为您发现它只会导致浏览器锁定。

What you need to do is to emulate a "modal" dialog box. 您需要做的是模拟一个“模态”对话框。

This is usually done by having your dialog box appear on top of another "overlay" element which importantly covers every other element, and prevents any user interaction with them. 通常,这是通过使对话框出现在另一个“覆盖”元素的顶部来完成的,该元素重要地覆盖了其他所有元素,并防止了用户与之交互。

It's also pretty hard to implement a confirm function that'll return a value - the window.confirm method can only do that because it's synchronous - it blocks all other JS processing while the dialog is displayed. 实现返回值的confirm函数也很困难window.confirm方法只能同步,因此只能执行此操作-在显示对话框时,它会阻止所有其他JS处理。

The easiest approach is to instead supply a callback function that'll get called once the user has selected the desired value. 最简单的方法是提供一个回调函数,一旦用户选择了所需的值,该函数就会被调用。

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

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