热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在条件下点燃所有事件,否则不点火-Onconditionfireallevents,elsefirenone

Iamworkingontopofexistingjavascript(thatIcannotalter)andneedtochecksomethingonsub

I am working on top of existing Javascript (that I can not alter) and need to check something on submit.

我正在处理现有的Javascript(我无法改变),需要检查提交的内容。

If my condition is true, no other submit handlers should be applied.

如果我的条件为真,则不应该应用其他提交处理程序。

If my condition is false, all other submit handlers shall be applied.

如果我的条件为假,则应适用所有其他提交处理程序。

What I have tried so far:

到目前为止我尝试了什么:

var form = jQuery('form'),
that = this,
events = $._data(form[0]).events['submit'];

form.off('submit');
form.on('submit', function (event) {
    if (that.myCondition()) {
        event.preventDefault();
        event.stopPropagation();
        return false;
    } else {
       console.log('Call these: ', events);
    }
});

Now eventsis always empty at that point.

现在,事件总是空着的。

It is empty as soon as I call form.off('submit')and I didn't get it to work with deep cloning either.

一旦我调用form.off('submit')它就是空的,我也没有让它与深度克隆一起工作。

Update: In this jsfiddle you see that both events are fired. I want one (preferably one that i add LAST) to be fired and prevent the other one from firing.

更新:在这个jsfiddle中你会看到两个事件都被触发了。我想要一个(最好是我加上LAST的一个)被射击并阻止另一个射击。

2 个解决方案

#1


1  

If I got your requirement correctly, you can try a nasty hack like

如果我正确地得到了你的要求,你可以试试一个讨厌的黑客

//the counter is just to demonstrate any condition
var counter = 0;
$('form').submit(function (e) {
    console.log('master handler form');
    //if counter is a multiple of 2 prevent other handlers
    if (++counter % 2 == 0) {
        //stop other submit handlers attached to the form
        e.stopImmediatePropagation();
        //stop propagation and default behavior
        return false;
    }
});

var form = jQuery('form'),
    events = $._data(form[0]).events['submit'];

var tmp = events.pop();
events.unshift(tmp);

Demo: Fiddle

#2


1  

Try utilizing $.Callbacks("stopOnFalse")

尝试使用$ .Callbacks(“stopOnFalse”)

var callbacks = $.Callbacks("stopOnFalse");
// if `input.val().length === 0` `fn1` not called
var fn2 = function fn2(event) {
    // `this`:`form` element
    // console.log(this); 
    event.preventDefault();
    event.stopPropagation();
    console.log('1st submit called:', input.val().length);
    if (input.val().length === 0) {
        // `input.val().length === 0`
        // do stuff , do not do stuff ? here
        output1.html('Input needed!');
        // reset `output2` `html`
        output2.html("...");
        // return `false` to "stop" `callbacks` queue;
        // `fn1` not called
        return false;
    }
};
// if `input.val().length !== 0` , `fn1` called,
// else `fn1` not called
var fn1 = function fn1(event) {
    // do stuff 
    // call `event.preventDefault();` , here, if needed;
    // else call `output2.html('success!');` , submit form
    // event.preventDefault();
    console.log('2nd submit called:', input.val().length);
    output2.html('success!');
};
// add `fn2` , `fn1` to `callbacks` queue
callbacks.add(fn2, fn1);
// call both `fn2` , `fn1` if `input.val().length !== 0` ,
// else, do not call `fn1`
form.on('submit', function (event) {
    // `fire` `callbacks` `fn2` , `fn1` with context `this`:`form` ,
    // pass `event` as parameter to `fn2`, `fn1`
    callbacks.fireWith(this, [event])
});

jsfiddle http://jsfiddle.net/bkdm7fzt/3/


推荐阅读
author-avatar
许琼博762375
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有