Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在触发事件之前等待用户确认_Javascript_Twitter Bootstrap_Bootstrap Modal - Fatal编程技术网

Javascript 在触发事件之前等待用户确认

Javascript 在触发事件之前等待用户确认,javascript,twitter-bootstrap,bootstrap-modal,Javascript,Twitter Bootstrap,Bootstrap Modal,我试图弄清楚事件是如何工作的,以及如何在执行原始事件之前等待第二个事件触发 $.fn.customConfirm = function (message){ // Create modal // then ... $(confirmModal).find("modal-footer a").click(function(e2){ e2.preventDefault(); return ($(this).hasClass("d

我试图弄清楚事件是如何工作的,以及如何在执行原始事件之前等待第二个事件触发

 $.fn.customConfirm = function (message){
     // Create modal
     // then ...
     $(confirmModal).find("modal-footer a").click(function(e2){
          e2.preventDefault();
          return ($(this).hasClass("dialogConfirm")); // Returns true if option has class dialogConfirm
     });
 };

 $("a[data-confirm]", click(function(e){
      if (!$.customConfirm($(this).attr("data-confirm")))
          return false; // Only if customConfirm returns false
      // Continue
 });
这很有魅力
customConfirm
使用2个按钮(确认/取消)创建引导模式,并将
数据确认
属性值设置为模式正文html


我不知道如何解决的是如何根据用户与modal的交互处理事件
e
。到目前为止,它只显示模式对话框,而原始事件似乎没有任何作用。

我通过在单击的元素中添加属性
data confirmed
(如果用户确认),然后在原始元素上触发第二次
单击()
,解决了这个问题

$.fn.customConfirm = function (message){
      var handle = $(this);
      // Create modal
      // then ...
      $(confirmModal).find("modal-footer a").click(function(e2){
           e2.preventDefault();
           $(handle).attr("data-confirmed", ($(this).hasClass("dialogConfirm")))[0].click();
           return false;
      });
 };

$("a[data-confirm]").click(function(e){
     if ($(this).attr("data-confirmed") !== "true")
     {
           e.preventDefault();
           $(this).customConfirm();
           return false;
     }
     $(this).removeAttr("data-confirmed");

     // Continue
});

您必须设置一些东西,以便确认对话框本身知道如何触发某些操作。你不能像以前那样用一个简单的
if
测试来设置它,因为它是异步的。@Pointy所以自定义对话框不能充当JS
confirm()
事件吗?不,你不能让浏览器通过内置的
confirm()
来进行设置。但是,您可以将回调函数传递到确认对话框中,并让代码在单击按钮时调用它。