我想在 javascipt 中制作一个定制的确认框,就像内置的确认框一样。除非用户至少选择一件事,否则内置的确认框不允许代码继续进行。下面是我的代码: * ** * * 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 结束* ** * *
** * ** 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 结束* ** * *
** * **示例代码开始* ** * * 所以这就是我所期望的:
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
}
** * **示例代码结束* ** * *
我使用了循环,但它使线程完全被占用,并且没有让我有机会按下任何按钮,这很明显但是递归使您可以自由地执行其他活动。即使在按下确认按钮后,confirmResult 的值将变为 1,但我会通过警报进行检查。递归循环,即确认循环似乎没有将其读取为 1。它仍然继续为 -1。如果我在该确认循环中发出警报,则该值将被读取为 1。任何人都可以帮助我实现我开始的目标??????Ps=>抱歉这么大的问题!!!