有没有办法在javascript中重新触发线程的原始事件?

nbysray5  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(164)

我试图在继续之前引入一个模式来确认用户的意图。为了动态地执行此操作,我希望从一开始就触发原始调用堆栈。例如,如果有人提交了一份表格,而我正在这样做:

jQuery('form').submit(function() {
     var positive = sure("Are you sure you want to submit this form?");
     return positive;
 });

然后我的 sure 函数可能如下所示:

var confirmed_sure = false;
function sure(input) {
        if(confirmed_sure) return true;
        var html = jQuery('html');

        var code = "<div id='sure'>"+
                    "<div id='sure_inner'>"+
                        input+
                        "<br style='clear: both;' />"+
                        "<div id='sure_yes'>OK</div>"+
                        "<div id='sure_no'>Cancel</div>"+
                    "</div>"+
                "</div>"+
            "</div>";
        jQuery(html).append(jQuery(code));

        var orig = sure.caller; // trying to get the original trigger
        jQuery('#sure_yes').click(function() {
            confirmed_sure = true; // set this bit so when we recursively call below it won't loop
            jQuery('#sure').remove();

            setTimeout(function() {
                return orig(); // this is the part I want to re-fire the form submit
            });
        });
        jQuery('#sure_no').click(function() {
            jQuery('#sure').remove();
            return false;
        });

        return false;
}

注意,我有 orig() 调用,这是利用function.caller(我知道它已弃用;如果有更好的选择,我洗耳恭听!)尝试获取原始函数,但它没有按照我希望的方式工作。
我在devtools中看到在 var positive 这是第二次从 orig() 打电话,但它不重新提交表格。我认为这是因为我在submit()调用中执行匿名函数,而不是submit()调用本身。
我试过使用 sure.caller.caller , sure.caller.arguements.callee.caller , arugments.callee.caller.callerarguements.callee.caller.arguements.callee.caller 界定 orig ,但无论如何,我似乎无法重新触发原始提交查询。
所以我的问题是:是否有一种方法可以存储并执行以编程方式导致原始javascript堆栈的原始事件(我用的这个例子是 submit() ,但在其他情况下可能是这样 click() 或者别的什么。)
对话更新:是的,这在技术上是可能的,如果有黑客行为的话。它依赖于已折旧(但仍受支持)的调用方和参数。基本上只需要重新工作 orig 值为target+类型对,并调用该值以重新执行,如下所示:

var orig_target = new_confirm.caller.arguments[0].originalEvent.target;
  var orig_type = new_confirm.caller.arguments[0].originalEvent.type;
  jQuery('#sure_yes').click(function() {
       sure_confirmed = true;
       jQuery('#sure_confirm').remove();
       setTimeout(function() { jQuery(orig_target).trigger(orig_type); }, 1);
  });

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题