0

我有几个按钮,但根据选择的单选按钮,我希望发生两件事中的一件。例如

when buildings radio button is selected, and the button is clicked then shows all building but doesnt show parks. similarly when parks radio button is selected, and the button is clicked shows all parks but doesnt show buildings.

我想删除不需要的相应功能的 onclick 事件,我可以这样做。

$('#youLinkID').attr('onclick','').unbind('click');

但是,这将删除两个onclick 事件。

有没有办法只删除一个 onclick 事件,可能是按名称,或者有一种方法可以按名称添加回一个 onclick 事件?

编辑:所以我在下面使用了 Mark 的帮助并使用了命名空间,并想出了这个:

$('#showOver').bind('click.buildOver', function(){ 
        $(this).unbind('click.parkOver')
    });

但是不起作用,我知道它在调用它并在运行我的测试时工作,

.css({"backgroundColor":"black","color":"white"})

它似乎没有添加 buildOver() 函数的 onclick 事件

4

2 回答 2

2

您可以将命名空间用于unbind特定的单击处理程序。

$("a").bind("click.pick1", function () {
    alert('pick 1');
    $(this).unbind("click.pick1");
}).bind("click.pick2", function () {
    alert('pick 2');
});

jsfiddle 上的示例

于 2013-04-15T13:39:02.590 回答
0

检查这个:演示

<a href="#" onclick="alert('hello');">I produce an alert via onclick</a>
<a href="#" onclick="alert('hello');">Me too</a>

<a href="#" onclick="$('a').removeAttr('onclick');">I remove them</a>

诀窍是这段代码:

$('a').removeAttr('onclick');

参考

于 2013-04-15T13:38:46.560 回答