javascript - Jquery 每个等待模态关闭

标签 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() 没有等待用户与模式交互或模式关闭。

如何等待用户与模式交互或模式关闭?

最佳答案

我不知道你使用的模态库的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
}

关于javascript - Jquery 每个等待模态关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50157106/

相关文章:

android onTrimMemory 和 ComponentCallbacks2

javascript - 从回调函数访问数据

jquery - 使用切换按钮更新值

javascript - 绕过 XSRF token 检查

关闭浏览器后 jQuery 1.8.3 xsrf 错误

javascript - 格式化回调函数的返回值

javascript - 学习回调函数: Trying to understand different forms of callback

javascript - 是否可以(并且正确)调用返回数组并直接使用索引键的函数?

php - 更改文本输入值

javascript - for 循环的替代方案