2014-10-01 83 views
0

我对SimpleModal确认示例有问题。我想等待结果作为基本确认对话框,然后根据按下的按钮采取行动。我在其他js打电话。我的js代码是:simplemodal确认实例等待返回值

var res = confirm("Delete elements?"); 

if (res == true) { 
     mainController.deleteNode(nodeMoveMessage, url); 
    } 

,以及使对话框中的JS是:

function confirm(message, callback) { 
var modalWindow = document.getElementById("confirm"); 
console.log(modalWindow); 
$(modalWindow).modal({ 
    closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>", 
    position: ["20%",], 
    overlayId: 'confirm-overlay', 
    containerId: 'confirm-container', 
    onShow: function (dialog) { 
     var modal = this; 

     $('.message', dialog.data[0]).append(message); 

     // if the user clicks "yes" 
     $('.yes', dialog.data[0]).click(function() { 
      // close the dialog 
      modal.close(); // or $.modal.close(); 
      // call the callback 
      return true; 
     }); 
    } 
}); 
} 

在我的JSP中的DIV是:

<div id='confirm'> 
     <div class='header'><span>Confirm</span></div> 
     <div class='message'></div> 
     <div class='buttons'> 
      <div class='no simplemodal-close'>No</div><div class='yes'>Yes</div> 
     </div> 
    </div> 

非常感谢!

回答

0

你已经到了一个回调添加到您的模式,所以先在confirm功能添加:

function confirm(message, callback) { 
    var modalWindow = document.getElementById("confirm"); 
    console.log(modalWindow); 
    $(modalWindow).modal({ 
     closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>", 
     position: ["20%",], 
     overlayId: 'confirm-overlay', 
     containerId: 'confirm-container', 
     onShow: function (dialog) { 
      var modal = this; 

      $('.message', dialog.data[0]).append(message); 

      $('.yes', dialog.data[0]).click(function() { 
       modal.close(); $.modal.close(); 
       callback(); // HERE IS THE CALLBACK 
       return true; 
      }); 
     } 
    }); 
} 

然后你就可以调用confirm功能与执行的回调时按钮点击,这样的:

function doSomething() { 
    mainController.deleteNode(nodeMoveMessage, url); 
} 

confirm("Delete elements?", doSomething); 

现在,当用户点击按钮doSomething功能将被调用,元素将b删除。