[英]jQuery - Bind Unbind click event depending of condition
I need to disable the click event from a jQuery Mobile button depending if an array is empty or not. 我需要从jQuery Mobile按钮禁用click事件,具体取决于数组是否为空。 When the location loads I don't want the user to be able to click, but once the array has some values, I would like to change that. 当位置加载后,我不希望用户单击,但是一旦数组具有一些值,我想更改它。
This is what I have tried: 这是我尝试过的:
if (points.length != 0){ $('#addPlaces').bind('click');}
else{$('#addPlaces').unbind('click');}
I have also tried the same condition with: 我也尝试过相同的条件:
$('#addPlaces').on('click');
$('#addPlaces').off('click');
What am I doing wrong? 我究竟做错了什么? Thanks! 谢谢!
Why do you wanna do that. 你为什么要这么做。 Just return the event when the array is empty and continue with the execution otherwise. 只要在数组为空时返回事件,否则继续执行。
$('#addPlaces').on('click', function() {
if (points.length === 0) {
return
}
});
This way you need not keep toggling the event. 这样,您就不必继续切换事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.