2009-03-09 6 views
5

jquery 스크립트를 설정하는 버튼이있는 경우 스크립트가 완료 될 때까지 버튼이 비활성 상태인지 확인하는 방법이 있습니까? (아마 버튼의 클릭 이벤트에) 스크립트의 시작 부분에JQuery 스크립트로드 타이밍

답변

10

어딘가에는, true로 버튼의 disabled 속성을 설정

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

그런 다음, 완료, 그 속성을 제거 할 때 :

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

편집 :

(약간) 멋있게하려면 작업을 수행하는 동안 버튼의 텍스트를 변경하십시오. 이미지 버튼 인 경우 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; 
       } 
     } 
    }); 
}