admin管理员组文章数量:1023667
I need to perform some task based on 2 conditions on page load. 1) If a button is clicked or 2) If condition bees true
so can I bine this two events into one?
if(x== true) || button.click(function(e){
..
perform task...
}
I need to perform some task based on 2 conditions on page load. 1) If a button is clicked or 2) If condition bees true
so can I bine this two events into one?
if(x== true) || button.click(function(e){
..
perform task...
}
Share
Improve this question
asked Apr 8, 2014 at 14:55
user1298426user1298426
3,71715 gold badges62 silver badges116 bronze badges
3
- I assume you mean if the condition bees true after the initialisation code for the page has finished executing? How does the condition bee true? As a result of code you're calling? – Anthony Grist Commented Apr 8, 2014 at 14:56
- I am passing some variable from controller. If that is true or if button is clicked then perform some operation. – user1298426 Commented Apr 8, 2014 at 14:59
- This is not how you're supposed to handle events... You should have 1 handler for click, calling a method, and then you're free to call the same method on any consition you need, whenever you need it... – Laurent S. Commented Apr 8, 2014 at 14:59
3 Answers
Reset to default 3var perform_task = function() {
....
}
if(x == true) perform_task();
button.click(function(er) { perform_task(); }
try this?
button.click(function(){
if(x == true){
/* do something */
}
});
var flag = false;
$("button").click(function() {
flag = true;
});
if((x== true) || flag ){
// do here
}
I need to perform some task based on 2 conditions on page load. 1) If a button is clicked or 2) If condition bees true
so can I bine this two events into one?
if(x== true) || button.click(function(e){
..
perform task...
}
I need to perform some task based on 2 conditions on page load. 1) If a button is clicked or 2) If condition bees true
so can I bine this two events into one?
if(x== true) || button.click(function(e){
..
perform task...
}
Share
Improve this question
asked Apr 8, 2014 at 14:55
user1298426user1298426
3,71715 gold badges62 silver badges116 bronze badges
3
- I assume you mean if the condition bees true after the initialisation code for the page has finished executing? How does the condition bee true? As a result of code you're calling? – Anthony Grist Commented Apr 8, 2014 at 14:56
- I am passing some variable from controller. If that is true or if button is clicked then perform some operation. – user1298426 Commented Apr 8, 2014 at 14:59
- This is not how you're supposed to handle events... You should have 1 handler for click, calling a method, and then you're free to call the same method on any consition you need, whenever you need it... – Laurent S. Commented Apr 8, 2014 at 14:59
3 Answers
Reset to default 3var perform_task = function() {
....
}
if(x == true) perform_task();
button.click(function(er) { perform_task(); }
try this?
button.click(function(){
if(x == true){
/* do something */
}
});
var flag = false;
$("button").click(function() {
flag = true;
});
if((x== true) || flag ){
// do here
}
本文标签: javascriptHow to add click event and if condition togetherStack Overflow
版权声明:本文标题:javascript - How to add click event and if condition together? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745533584a2154859.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论