【问题标题】:can i create custom confirm box in pure javascript (without using jquery) work same as window.confirm?我可以在纯 javascript 中创建自定义确认框(不使用 jquery)与 window.confirm 一样吗?
【发布时间】:2014-09-04 15:09:31
【问题描述】:

confirm.js 中有类似这个页面的代码(http://www.developphp.com/view.php?tid=1385(dialog.js))

点击yes按钮后我想在php中做一些操作(比如从数据库中删除记录和提交表单),如果没有它应该返回false

这可能吗?

<script type="text/javascript" src="confirm.js"></script>
<script type="text/javascript">
function validation()
{
    if(document.getElementById('tbox').value=='')
    {
        alert('enter value');
        return false;
    }
    else
    {
        confirm.render('','','sub_btn');
    }
}
</script>
<form method="get">
    <table width="100%" height="500px" z-index="500000" style="background:yellow;">
        <tr>
            <td>
                <input type="text" id="tbox">
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" id="sub_btn" value="Submit" onclick="return validation()">
            </td>
        </tr>
    </table>
</form>

【问题讨论】:

  • “我可以在纯javascript中创建自定义确认框(不使用jquery)”:可以。 “和window.confirm一样工作吗?”:没有。
  • 这是我今天能很快找到的最佳选择github.com/dFelinger/PurePopup

标签: javascript


【解决方案1】:

否您不能完全复制默认的确认/警报/提示提示。 这些会阻止 javascript 运行超过该行,直到返回某些内容,这是 javascript 无法做到的。

那里有很多自定义模式对话框,为了解决您无法阻止 javascript 等待响应的事实,它们改为使用 回调。 您将一个函数传递给对话窗口的调用,然后在对话窗口完成后执行此函数。

一些例子有:

http://bootboxjs.com/

http://jqueryui.com/dialog/

一个快速的谷歌会给你更多吨。 我之前完全用 javascript 自己制作了一个,因为我不想拥有 css 文件。它没有其他功能那么全面,但它纯粹是 javascript,所以这里有一个链接: http://heuuuth.com/applications/JamModal.html

【讨论】:

  • 好吧,我已经看到所有提到的例子仍然不能做我想做的事
【解决方案2】:

没有。

无法在 DOM 中创建会阻塞 JavaScript 事件循环的对话。

当你显示它时,你必须停止你正在做的任何事情,然后让对话框中的选项重新开始你离开的过程。

【讨论】:

  • 但是我不知道该怎么做(我没听懂)
【解决方案3】:

您好,我的以下 cmets 已删除。所以现在我已经找到了解决方案,我将把它提供给 cmets 下面的所有人:

我在纯 javascript 中创建了一个自定义确认框代码,它看起来和工作都很棒。这太容易了。但问题是,如果我必须在同一个网页上获得用户的 2 次确认,那么我必须编写单独的一组函数(每组 2 个函数名称不同)。

我看不出回调如何帮助我解决问题。回调函数也会立即执行,无需等待检查用户是否点击了确认或取消按钮。

我可以将回调函数名称传递给另一个使用 setTimeOut() 的函数并等待 15 分钟让用户单击确认或取消吗?单击按钮后,我可以将用户的选择记录在全局变量中,并使用 clearTimeOut() 立即执行下一部分。 15 分钟后,如果没有得到用户响应,则将其视为响应取消,这应该是安全的。有人想尝试解决这个问题吗?

我不接触或阅读 jQuery、Angular 等。请不要让我参与这些讨论。

好的,这是解决方案。主文件是 customConfirm.htm :-

<html>
<head>
<style>
/* ~~~~~~~~~~~~~~~ Style for the pop box with blackener ~~~~~~~~~~~~~~~~~~~ */
.popBoxStyle { position: absolute; text-align: center;

visibility:hidden;
z-index: 2000;                                                              /* Nairit: Set high so that shows over page contents */
cursor: default;

background-color: #FFFFFF;
border:2px solid;
border-color: #6F7072;
border-radius:6px;                              /* More gives rounder edges */
box-shadow: 10px 10px 5px #606060;              /* Shadow can be used when black overlay is not used on the rest of the page. Or black has low opacity */

filter:alpha(opacity=100);                                          /* Works in IE */
opacity:1.0;                                                        /* Works in others. This is for the opacity of the box's background */

padding:3px;
}

.liner { clear:both; height:1px; background-color:#cccccc; }
.spacer { clear:both; height:5px; }

.black { position:absolute; z-index:1999; }

button.close { float:right; margin:5px; cursor:pointer; background-color:#d9534f !important; border-radius:4px !important; color:#fff !important; font-size:21px; font-weight:bold; line-height:1; color:#000000; }
/* ~~~~~~~~~~~~~ Style for the pop box with blackener ends ~~~~~~~~~~~~~~~~~~ */
</style>

<script language="javascript" src="PopBox.js"></script>
<script language="javascript">
var affirm = false; tm_ref=null, cb_func_name=null;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function customConfirm(func,msg)
{
initializePopBox();

popBox.innerHTML = "<table width='100%' cellspacing='0' cellpadding='5' border='0'>" +
"<tr><td>CONFIRM</td>" +
    "<td><button type='button' class='close' style='float:right;margin:5px;' onClick='setUserResponse(false);'>x</button></td></tr>" +
"<tr><td colspan='2'><div class='liner'></div></td></tr>" +
"<tr><td colspan='2'>"+msg+"</td></tr>" +
"<tr align='right'>" +
    "<td><input type='button' name='b_cancel' value='Cancel' onClick='setUserResponse(false);'></td>" +
    "<td><input type='button' name='b_confirm' value='Confirm' onClick='setUserResponse(true);'></td></tr>" +
"</table>";

aWidth = 300; aHeight = 150;
finalizePopBox(50);
cb_func_name = func;                        // Storing the callback function's name globally
tm_ref = setTimeout(func, 30000);           // 60000 milli seconds = 1 minute. 600000 milli seconds = 10 minutes. 900000 milli seconds = 15 minutes
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function handleConfirmResponse()
{
hidePop();
    if( affirm )
    {
    alert("He said Yes");       // Do your work here
    }

    else
    {
    alert("He said No");        // Do nothing
    }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function setUserResponse(respo)
{
affirm = respo;
clearTimeout(tm_ref);
cb_func_name();                 // Gets the function name from global and calls it
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
</script>
</head>

<body>
<div id="blackener" class="black"></div>
<div id="popContainer" class="popBoxStyle"></div>                   <!-- NK: The pop-up appears in this div -->

<input type="button" name="b1" value="Delete" onClick="customConfirm(handleConfirmResponse,'Are you sure?');">

</body>
</html>

支持文件是 PopBox.js :-

var popBox, popBoxStyl, BlackRef, aWidth, aHeight;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function initializePopBox()
{
popBox = document.getElementById("popContainer");
popBoxStyl = document.getElementById("popContainer").style;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function finalizePopBox(tp)     // Height from page top
{
popBoxStyl.width = aWidth + "px";
popBoxStyl.height = aHeight + "px";
popBoxStyl.left = parseInt(  ( (document.body.clientWidth - aWidth)/2 )  ) + "px";
    if(tp)
    popBoxStyl.top = tp;
    else
    popBoxStyl.top = parseInt(  ( (document.body.clientHeight - aHeight)/2 )  ) + "px";

popBoxStyl.visibility = "visible";          // Nairit: So they are using .style.visibility = . Not .style.display = which I use normally. Learn this way.
blacken();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function blacken()
{
BlackRef = document.getElementById("blackener");

var ht = document.body.scrollHeight;                // document.body.scrollHeight > document.body.offsetHeight > document.body.clientHeight
var wd = document.body.offsetWidth;                 //alert(ht); alert(wd);

BlackRef.style.left = "0px";
BlackRef.style.top = "0px";
BlackRef.style.height = ht + "px";
BlackRef.style.width = wd + "px";

BlackRef.style.backgroundColor = "#000000";
BlackRef.style.opacity = "0.5";
BlackRef.style.filter = 'alpha(opacity=50)';

BlackRef.style.display = "block";
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function hidePop()
{
popBoxStyl.visibility = "hidden";

BlackRef.style.backgroundColor = "#ffffff";
BlackRef.style.opacity = "1.0";
BlackRef.style.filter = 'alpha(opacity=100)';

BlackRef.style.display = "none";
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我从同一个网页的 2 个不同位置成功调用了 customConfirm()。我必须传递 2 个不同的响应处理程序回调函数名称。在调用 customConfirm() 之前,我必须将其值在相应的回调函数中需要的任何变量设为全局变量。但值得付出努力;-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 2021-07-15
    • 2021-10-05
    • 1970-01-01
    相关资源
    最近更新 更多