首页 > 解决方案 > Jquery 每个等待模式关闭

问题描述

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

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

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

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

标签: javascriptjquerycallbackmodal-dialogeach

解决方案


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

推荐阅读