简体   繁体   English

我可以在事件中调用另一个事件处理程序吗?

[英]Is it possible that I can call another event handler inside an event?

I have these two set of codes, one is to prompt whenever the button is clicked, 我有这两组代码,一个是在点击按钮时提示,

$('.button_class').click(function(){
    var baker_url = base_url + 'baker/baker/manage/Bread/1';
    var answer = confirm('Are you sure?');
    if(answer == true){
        // if this is true, i will call an external script so that within this condition that process will be executed
    }
    else if(answer == false){
        return false; // exits
}
});

and then this piece of code right here is the one responsible for executing the process when the button is clicked, 然后这个代码就是负责在单击按钮时执行进程的代码,

$('.another_button_class').click(function(e){
        e.preventDefault();
        var baker_url = base_url + 'baker/baker/manage/Bread/1';
        var form    = '#'+ $(this).attr('rel');
        var url     = $(form).attr('action');
        var target  = '#'+ $(form).attr('rel');
            $(target).slideUp();
            $.post(url, $(form).serialize(),function(data) {
                $(target).html(data).slideDown(function(){
                    if(data == '<div class="ok">Added</div>'){
                    setTimeout(refresh,1000)
                    c();
                    window.location = baker_url; // sets the url after refreshing
                    }
                });

            });
    });

in the first function, when answer == true , i want to execute the latter function ( the one with the process execution when button is clicked ). 在第一个函数中,当answer == true ,我想执行后一个函数( 单击按钮时执行流程的函数)。

Is it really possible that I can call an another function or event handler to execute its process inside another function? 我真的可以调用另一个函数或事件处理程序来在另一个函数中执行它的进程吗?

If I understand the question correctly, you have an effect that needs to happen either when you click button #2 or when you click button #1 AND click yes in a prompt? 如果我正确理解了这个问题,那么当您单击按钮#2或单击按钮#1并在提示中单击是时,您需要产生效果? If so I can think of three options: 如果是这样,我可以想到三个选择:

  1. Name the second function and make it work if e is undefined; 命名第二个函数,如果e未定义则使其工作; all it really does with it is prevent default. 所有它真正做到的是防止默认。 You could then call the named function from the first event. 然后,您可以从第一个事件中调用命名函数。

  2. Better version of 1: Make a new function that does everything except e.preventDefault(); 更好的版本1:创建一个除e.preventDefault();之外的所有功能的新功能e.preventDefault(); in the second piece of code. 在第二段代码中。 You could then call this from both events. 然后你可以从这两个事件中调用它。

  3. Use jQuery to simulate a click event for the second button, if 'yes' is selected at the first one. 如果在第一个按钮中选择“是”,则使用jQuery模拟第二个按钮的单击事件。

EDIT The second one would be preferable in this case. 编辑在这种情况下,第二个是更好的选择。 Use the third one if you actually need the event object for something. 如果您确实需要事件对象,请使用第三个。

Not sure if that is what you want 不确定这是不是你想要的

if(answer == true){
    // if this is true, i will call an external script so that within this condition that process will be executed
    $('.another_button_class').trigger('click');
}

This will trigger a call the click event handlers bound on the $('.another_button_class') element. 这将触发对$('.another_button_class')元素绑定的click事件处理程序的调用。


make sure to use e.preventDefault(); 确保使用e.preventDefault(); on the first click as well, or the link will be followed 在第一次点击时,或将遵循链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM