简体   繁体   中英

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 ).

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? If so I can think of three options:

  1. Name the second function and make it work if e is undefined; 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(); 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.

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.


make sure to use e.preventDefault(); on the first click as well, or the link will be followed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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