如何从确认弹出警报中获取返回值

时间:2015-12-03 10:40:25

标签: javascript

我正在尝试发出弹出警报。 它的问题是我无法获得返回值。 这是因为close函数是在函数本身中创建的。

有没有办法返回值?

JSFiddle

HTML

private Stream<Thing> allThings(Path path) throws FileNotFoundException, ParsingException, IOException {
    Elements things = builder.build(new FileInputStream(path.toFile()))
            .getRootElement().getChildElements();
    return IntStream.range(0, things.size()).mapToObj(i -> new Thing(things.get(i)));
}

JS

<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>

CSS

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;
    }
  }
}

3 个答案:

答案 0 :(得分:2)

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

如果您希望能够处理用户输入,则应创建执行onclick事件的回调函数,如用户gurvinder372在其答案中所述。

答案 1 :(得分: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;
        }
    }
}

答案 2 :(得分:0)

如何使用确认框而不是警告来使用IF语句执行相同的功能?

$sql = "UPDATE my_table SET my_field = my_field+1 WHERE my_other_field = ?";
$this->db->query($sql, array('test'));