最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible that I can call another event handler inside an event? - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited May 25, 2012 at 9:46 Tsukimoto Mitsumasa asked May 25, 2012 at 9:05 Tsukimoto MitsumasaTsukimoto Mitsumasa 5824 gold badges20 silver badges44 bronze badges 4
  • 1 External script in the sense?? – FrontEnd Expert Commented May 25, 2012 at 9:11
  • by external I mean, another event handler will be called to execute certain processes. – Tsukimoto Mitsumasa Commented May 25, 2012 at 9:20
  • 1 A script is a whole execution context, and "external" means it's located in its own file. Please use the terms "function" and "(event) handler". – Bergi Commented May 25, 2012 at 9:44
  • @Bergi , I'm sorry man, I promise I'll use the proper terms. Thanks. – Tsukimoto Mitsumasa Commented May 25, 2012 at 9:45
Add a ment  | 

2 Answers 2

Reset to default 3

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

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.

发布评论

评论列表(0)

  1. 暂无评论