2015-12-03 93 views
1

我正在尝试进行弹出式警示。 它的问题是我无法获得返回值。 这是因为close函数是在函数本身中创建的。如何从确认弹出警报获取返回值

有没有办法返回值?

JSFiddle

HTML

<button id="div" onclick="popup()">START</button> 

<div id="popUp"> 
    <div id="popUpHeader"> 
    </div> 
    <div id="popUpBody"> 
    <h1 id="title"></h1> 
    <p id="txt"></p> 
    </div> 
    <button id="popUpButton">OK</button> 
    <button id="popUpYes">YES</button> 
    <button id="popUpNo">NO</button> 
</div> 

JS

function popup() { 
    var a = popupbox('Warning', 'Are you sure?', 'yesno'); 
    alert(a); 
} 

function popupbox(title, txt, type) { 
    document.getElementById('popUp').style.display = 'block'; 
    document.getElementById('title').innerHTML = title; 
    document.getElementById('txt').innerHTML = txt; 

    if(type == 'ok') { 
    document.getElementById('popUpButton').style.display = 'block'; 
    document.getElementById('popUpButton').onclick = function() { 
     document.getElementById('popUp').style.display = 'none'; 
     document.getElementById('overlay').style.display = 'none'; 
    } 
    } else { 
    document.getElementById('popUpYes').style.display = 'block'; 
    document.getElementById('popUpNo').style.display = 'block'; 
    //return true; 
    document.getElementById('popUpYes').onclick = function() { 
     document.getElementById('popUp').style.display = 'none'; 
     return true; 
    } 
    document.getElementById('popUpNo').onclick = function() { 
     document.getElementById('popUp').style.display = 'none'; 
     return false; 
    } 
    } 
} 

CSS

#popUp { 
    display: none; 
    position: absolute; 
    top: 25%; 
    left: 50%; 
    margin-left: -100px; 
    background: white; 
    width: 250px; 
    min-height: 100px; 
    border-radius: 4px 4px 4px 4px; 
    border: 1px solid #888; 
    border-top: 5px solid black; 
    box-shadow: 0px 2px 10px #999; 
    z-index: 9999; 
    text-align: center; 
} 
#popUpBody { 
    margin: 3px 0px 3px 0px; 
} 
#popUpBody h1 { 
    margin-top: 10px; 
    font-family: 'Roboto', sans-serif; 
    font-size: 15px; 
} 
#popUpBody p { 
    padding: 10px 10px 0px 10px; 
    color: black; 
    font-family: 'Roboto', sans-serif; 
    font-size: 15px; 
    margin: 0 auto; 
    /*ADJUST HEIGHT ACCORDING CONTENTS*/ 

    margin-bottom: 50px; 
} 
#popUpButton { 
    display: none; 
    position: absolute; 
    bottom: 0px; 
    width: 60px; 
    margin: 10px 0px 10px 60px; 
    padding: 5px; 
    font-size: 14px; 
} 
#popUpYes { 
    display: none; 
    position: absolute; 
    bottom: 0px; 
    width: 60px; 
    margin: 10px 0px 10px 40px; 
    padding: 5px; 
    font-size: 14px; 
} 
#popUpNo { 
    display: none; 
    position: absolute; 
    bottom: 0px; 
    width: 60px; 
    margin: 10px 0px 10px 110px; 
    padding: 5px; 
    font-size: 14px; 
} 
+0

你不应该使用'prompt'吗? – Danield

+0

我想使用自制提示盒 – Ethannn

回答

1

由于您的语句被执行后,用户输入给出,你需要使用回调处理程序正如我在3号线已经更新

更新了fiddle

function popup(){ 
var a = popupbox('Warning','Are you sure?','yesno', function(){alert('yes')}, function(){alert('no')}); 
    //alert(a); 
} 



function popupbox(title,txt,type, okCallback, noCallBack){ 
    document.getElementById('popUp').style.display = 'block'; 
    document.getElementById('title').innerHTML = title; 
    document.getElementById('txt').innerHTML = txt; 

    if(type == 'ok'){ 
     document.getElementById('popUpButton').style.display = 'block'; 
     document.getElementById('popUpButton').onclick = function(){ 
      document.getElementById('popUp').style.display = 'none'; 
      document.getElementById('overlay').style.display = 'none'; 
     okCallback(); 
     } 
    } else { 
     document.getElementById('popUpYes').style.display = 'block'; 
     document.getElementById('popUpNo').style.display = 'block'; 
     //return true; 
     document.getElementById('popUpYes').onclick = function(){ 
      document.getElementById('popUp').style.display = 'none'; 
     okCallback(); 
      return true; 
     } 
     document.getElementById('popUpNo').onclick = function(){ 
      document.getElementById('popUp').style.display = 'none'; 
     noCallBack(); 
      return false; 
     } 
    } 
} 
+0

感谢gurvinder,我没有意识到回调函数。现在有些事要读。 – Ethannn

+0

@Ethannn很乐意帮忙 – gurvinder372

+0

我玩过它,但它似乎不能将一个值作为变量返回。有另一种方法吗? – Ethannn

0

怎么样使用一个确认框,而不是一个警报的做同样的功能用IF语句?

var s = confirm('Are you sure? :'); 

    if (s == true) { 

} 
+0

就像我说的我想使用定制/自制的确认框 – Ethannn

2

的问题是,你的popupbox函数返回imideatly。 在你的弹出框功能中,你设置了具有返回值的onclick事件。这不会影响您的popupbox函数的返回值(因为onclick函数在用户单击时异步执行;)。

如果你想能够处理用户输入,你应该创建回调函数,用户gurvinder372在他的回答中解释了执行onclick事件。

+0

hello @ user3656699这个回调函数没有办法。谢谢! – Ethannn