2009-03-09 4 views
5

jqueryスクリプトを設定するボタンがある場合は、スクリプトが完了するまでボタンが非アクティブであることを確認する方法がありますか?どこか、スクリプトの先頭に(おそらく、ボタンのクリックイベントの)JQueryスクリプトのロードタイミング

答えて

10

、trueにボタンのdisabled属性を設定します。

$("#mybutton").attr("disabled", true); 

そして、完全に、その属性を削除します。

$("#mybutton").removeAttr("disabled"); 

EDIT:

(少し)気に入った場合は、作業中にボタンのテキストを変更します。イメージボタンの場合は、srcをフレンドリーな「お待ちください」というメッセージに変更することができます。ここでは、ボタンのテキスト版の例です:

$("#mybutton").click(function() { 
    var origVal = $(this).attr("value"); 

    $(this).attr("value", "Please wait..."); 
    $(this).attr("disabled", true); 

    //Do your processing. 

    $(this).removeAttr("disabled"); 
    $(this).attr("value", origVal); 
}); 
33

これは、私はjQueryのを拡張したいとつの領域です:

$.fn.disable = function() { 
    return this.each(function() { 
     if (typeof this.disabled != "undefined") this.disabled = true; 
    }); 
} 

$.fn.enable = function() { 
    return this.each(function() { 
     if (typeof this.disabled != "undefined") this.disabled = false; 
    }); 
} 

、その後、あなたが行うことができます。

$("#button").disable(); 
$("#button").enable(); 

私は自分自身を見つけます無効化/有効化は多くを制御します。

+0

私はこの解決策を推奨します。あなたは簡単にテキストを変更する機能を強化することもできます。 – Raithlin

3

ありがとうございました。私はあなたの機能を使用し、自分自身を作成し​​て無効になった要素を切り替えることができました。

$.fn.toggleDisable = function() { 
    return this.each(function() { 
     if (typeof this.disabled != "undefined"){ 
       if(this.disabled){ 
        this.disabled = false; 
       }else{ 
        this.disabled = true; 
       } 
     } 
    }); 
}