admin管理员组文章数量:1315951
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
2 Answers
Reset to default 3Not 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:
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.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.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.
本文标签: javascriptIs it possible that I can call another event handler inside an eventStack Overflow
版权声明:本文标题:javascript - Is it possible that I can call another event handler inside an event? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741995898a2410037.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论