2009-04-18 42 views
3

以下作品中的JavaScript jQuery代码,但我想2层的功能添加到该按钮的状态。jQuery的变化股利按钮状态和单击禁用

  1. 当用户单击其中一个按钮时,未点击的另一个按钮将获得一个新类(外观)。

  2. 两个按钮的状态应更改为不可点击。

 
[div id="1" class="__button_image"] [/div] 
[div id="2" class="__button_image"] [/div] 
 
$("div.__button_image").mouseover(function() { 
    $(this).addClass("__button_image_hover"); 
}); 

$("div.__button_image").mouseout(function() { 
    jq(this).removeClass("__button_image_hover"); 
}); 

$("div.__button_image").click(function() { 
    $(this).removeClass("__button_image_hover"); 
    $(this).addClass("__button_image_clicked"); 

    jQuery.get('/do/request');   
}); 

回答

3

下面是最后的代码我去:

$("div.__button_image").mouseover(function() { 
    $(this).addClass("__button_image_hover"); 
}); 

$("div.__button_image").mouseout(function() { 
    $(this).removeClass("__button_image_hover"); 
}); 

$("div.__button_image").click(function() { 

    /** change button look to 'clicked' */ 
    $(this).addClass("__button_image_clicked"); 

    /** get the id of the current button */ 
    b_id = $(this).attr('id'); 

    /** unbind both vote buttons for *no* interaction */ 
    $("div.__button_image").unbind('click'); 
    $("div.__button_image").unbind('mouseover'); 
    $("div.__button_image").unbind('mouseout'); 

    /** 
    * wire the .each function to iterate the classes 
    * so we can change the look of the one that was 
    * not clicked. 
    */ 
    $('div.__button_image').each(function() { 
     button_id = $(this).attr('id'); 
     if(button_id!=b_id) { 
     $('#'+button_id).removeClass("__button_image"); 
     $('#'+button_id).addClass("__button_image_gray"); 

    } 
}); 

jQuery.get('/do/request?id='+b_id); 
$(this).parent().css('cursor', 'default'); 
2

什么问题?我只能看到你缺少的是

$("div.__button_image").unbind('click'); 

这将删除'click'处理程序(将其设置回默认值)。

+0

感谢解除绑定的意见,我把它更进一步,说: .unbind( '点击'); .unbind('mouseover'); .unbind('mouseout'); 要完全禁用按钮相互作用,一旦用户点击这两个按钮中的一个。 – jdev 2009-04-20 21:44:01

+1

您可以使用.unbind()(不带任何参数)来解除所有绑定事件。 – 2009-06-30 11:32:45

1

我想你的点击()处理程序改成这样:

$("div.__button_image").click(function() { 
    $(this).removeClass("__button_image_hover"); 
    $(this).addClass("__button_image_clicked"); 

    /* 
    * Add look class to all buttons, then remove it from this one 
    */ 
    $("div.__button_image").addClass("look"); 
    $(this).removeClass("look"); 

    /* 
    * Remove click handler from all buttons 
    */ 
    $("div.__button_image").unbind('click'); 

    jQuery.get('/do/request');   
}); 
1

这总是对我的作品(也改变不透明度为80%并将光标更改为等待)

$("#buttonDivId").css({opacity: 0.8, cursor: "wait"}).prop("disabled", true);