programing

모두 비활성화하는 방법모두 비활성화하는 방법jQuery와 함께 폼 안에?jQuery와 함께 폼 안에?

lovejava 2023. 8. 17. 20:30

모두 비활성화하는 방법jQuery와 함께 폼 안에?

<form id="target">
....
</form>

이전 버전에서는 을 사용할 수 있습니다.jQuery 1.6 기준으로 다음을 대신 사용해야 합니다.

$("#target :input").prop("disabled", true);

'target' 내부의 모든 폼 요소를 비활성화합니다.참조:

모든 입력, 텍스트 영역, 선택 및 버튼 요소와 일치합니다.

당신이 원하는 것이.<input>요소:

$("#target input").prop("disabled", true);

위의 예는 기술적으로 올바르지 않습니다.최신 jQuery에서는prop()방법은 장애인과 같은 것들에 사용되어야 합니다.해당 API 페이지를 참조하십시오.

'target' 내부의 모든 양식 요소를 비활성화하려면 모든 입력, 텍스트 영역, 선택 및 버튼 요소와 일치하는 :input 선택기를 사용합니다.

$("#target :input").prop("disabled", true);

요소만 원하는 경우 이것을 사용합니다.

$("#target input").prop("disabled", true);

또한 보다 간결한 방법은 실렉터 엔진을 사용하는 것입니다.따라서 Div 또는 Form 부모의 모든 Form 요소를 비활성화합니다.

$myForm.find(':input:not(:disabled)').prop('disabled',true)

추가할 수 있습니다.

 <fieldset class="fieldset">

그리고 나서 당신은 전화할 수 있습니다.

 $('.fieldset').prop('disabled', true);

이 한 줄로 양식의 입력 필드를 비활성화할 수 있습니다.

$('form *').prop('disabled', true);

쓰기만큼 쉬운 모든 양식을 비활성화하려면:

jQuery 1.6+

$("#form :input").prop("disabled", true);

jQuery 1.5 이하

$("#form :input").attr('disabled','disabled');

다음과 같이 할 수 있습니다.

//HTML BUTTON
<button type="button" onclick="disableAll()">Disable</button>

//Jquery function
function disableAll() {
    //DISABLE ALL FIELDS THAT ARE NOT DISABLED
    $('form').find(':input:not(:disabled)').prop('disabled', true);

    //ENABLE ALL FIELDS THAT DISABLED
    //$('form').find(':input(:disabled)').prop('disabled', false);
}

Gnarf는 최종 답변(버전 1.6에서 jQuery api에 대한 변경 사항 포함)을 제공했습니다.

언급URL : https://stackoverflow.com/questions/1416900/how-to-disable-all-input-inside-a-form-with-jquery