[英]jQuery unbinding click event on a radio button that is already selected
我正在尝试创建一些参数过滤器,但我不想在已经选择单选按钮过滤器时调用 function 来使我的搜索 AJAX 调用。
$('#contentType input:enabled').bind('click', function(e){
var $this = $(this),
$type = this.type,
$id = this.id;
if ($type === 'radio') {
if ($id === 'allContent'){
sFrm.$boxes.attr({'checked': false});
sFrm.$specificRadio.attr({'disabled': true});
searchAjax();
removeFilter();
}
} else if ($type === 'checkbox') {
if (sFrm.$boxes.filter(':checked').length === 0) {
sFrm.$allRadio.trigger('click');
} else {
var filterConfig = {
index: $('.subFilter').index($this),
txt: jQuery.trim($this.parent().text())
}
sFrm.$allRadio.attr({'checked': false});
sFrm.$specificRadio.attr({'disabled': false, 'checked': true});
searchAjax();
if ($this.is(':checked')) {
addFilter(filterConfig);
} else {
removeFilter(filterConfig.index);
}
}
}
});
因此,上面的 jQuery 查看集合中所有启用的输入,当用户单击allContent单选按钮时,将调用searchAjax function。
当尚未选择/选中所述单选按钮时,这是正确的。 目前 function 仍然在选择按钮时被调用,我想停止这种行为但不知道怎么做?
编辑
不确定我解释正确。 希望这个 jsFiddle 会有所帮助:
基本上,如果已经检查了所有内容类型并在检查时单击,则不应调用searchAjax 。 它当前调用 function。
当它未被选中并单击时,它应该像现在一样运行(禁用同级单选按钮并取消选中所有关联的复选框)。
在处理程序的顶部添加:
if ($this.is(":checked")) {
return;
}
如果当前选择了单选按钮,则事件处理程序将返回并且不执行 function 的 rest。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.