質問

以下の JavaScript jQuery コードは機能しますが、ボタンの状態に 2 つの機能を追加する必要があります。

  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');        
});
役に立ちましたか?

解決

ここで私が一緒に行った最終的なコードは次のとおりです。

$("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');

他のヒント

問題は何ですか?私はあなたが不足していることを見ることができる唯一の事はある。

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

このハンドラ(デフォルトに戻す設定)を「クリック」削除されます。

私はこれまで、あなたのクリック()ハンドラを変更します

$("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');        
});
常に

この私の作品(これも80%に不透明度を変更し、待つためにカーソルを変更)

$("#buttonDivId").css({opacity: 0.8, cursor: "wait"}).prop("disabled", true);
あなたはjQueryのUIを使用している場合は、

あなたは無効にする方法を使用することができます。

$("selector").button("disable");

http://api.jqueryui.com/button/する

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top