开发者

One button is assigned two functions in jQuery, calls both. How can I make it call one of them?

开发者 https://www.devze.com 2023-02-02 09:12 出处:网络
I have two buttons that have assigned two functions, one by a broad selection: $(\".buy_button a\").click(function() {

I have two buttons that have assigned two functions, one by a broad selection:

$(".buy_button a").click(function() {

and one with a more specific selection:

$(".under_form_xml .buy_button a").click(function(){

The end result is that the last button 开发者_开发知识库executes both functions... I thought I overwrote the functions by being more specific, like css.

is there any way I can "clear" the specific selection of previous attached functions?

I solved this by renaming the second class, but I'm curious if there is any way this can be solved with the existing names?


.unbind() is what you're looking for.

first unbind the attached event to the second button and then assign the new event handler.

http://api.jquery.com/unbind/


Jani's answer is correct if you want to clear the events associated with elements that either contain both or one of those classes.

Let's say if you still want to search for the element that contains only both classes then simply using $(".under_form_xml.buy_button a") should work.

Hopefully that solves your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消