Jquery每个等待模态关闭

时间:2018-05-03 14:08:55

标签: javascript jquery callback modal-dialog each

我试图在$.each()循环中显示模态对话框,并且在每次迭代中我都在动态地更改模态的数据。

$.each(real_names, function (key, value) {
      $('#restore-modal').find('.asset_name').text(value.relative_name);
      $('#restore-modal').modal('open');
}

问题是$.each()不等待用户与模态或模态进行交互以使其关闭。

我如何等待用户与模态或模态进行交互才能关闭?

1 个答案:

答案 0 :(得分:1)

我不知道您使用的模态库的API是什么,但您可以尝试使用递归函数,使用其计数器,它会允许您循环遍历real_names数组,同时让您完全控制循环:

var counter = 0;

function openModal() {
  // first a mecanism to escape the recursion:
  if (counter === real_names.length) {
    return;
  }

  // not sure about your code here, but for the idea:
  $('#restore-modal').find('.asset_name').text(real_names[counter].relative_name);
  $('#restore-modal').modal('open');

  // here, depending on how your modal API works, you can call again 
  // the openModal function after the desired event (user closing modal,
  // clicking on confirmation...),
  // first incrementing the counter for searching the next modal

  // or make the function return, to escape the recursion for any other event
}