IT TIP

jquery에서 드롭 다운 상자 활성화 / 비활성화

itqueen 2020. 11. 22. 21:03
반응형

jquery에서 드롭 다운 상자 활성화 / 비활성화


jQuery를 처음 사용하고 확인란을 사용하여 드롭 다운 목록을 활성화 및 비활성화하고 싶습니다. 이것은 내 HTML입니다.

<select id="dropdown" style="width:200px">
    <option value="feedback" name="aft_qst">After Quest</option>
    <option value="feedback" name="aft_exm">After Exam</option>
</select>
<input type="checkbox" id="chkdwn2" value="feedback" />

이 작업을 수행하려면 어떤 jQuery 코드가 필요합니까? 또한 좋은 jQuery 문서 / 연구 자료를 검색합니다.


이해하기 쉬운 한 가지 방법이 있습니다.

http://jsfiddle.net/tft4t/

$(document).ready(function() {
 $("#chkdwn2").click(function() {
   if ($(this).is(":checked")) {
      $("#dropdown").prop("disabled", true);
   } else {
      $("#dropdown").prop("disabled", false);  
   }
 });
});

시도-

$('#chkdwn2').change(function(){
    if($(this).is(':checked'))
        $('#dropdown').removeAttr('disabled');
    else
        $('#dropdown').attr("disabled","disabled");
})

나는 JQuery> 1.8을 사용하고 있으며 이것은 나를 위해 작동합니다 ...

$('#dropDownId').attr('disabled', true);

$("#chkdwn2").change(function(){
       $("#dropdown").slideToggle();
});

JsFiddle


이 시도

 <script type="text/javascript">
        $(document).ready(function () {
            $("#chkdwn2").click(function () {
                if (this.checked)
                    $('#dropdown').attr('disabled', 'disabled');
                else
                    $('#dropdown').removeAttr('disabled');
            });
        });
    </script>

활성화 / 비활성화하려면-

$("#chkdwn2").change(function() { 
    if (this.checked) $("#dropdown").prop("disabled",true);
    else $("#dropdown").prop("disabled",false);
}) 

데모-http: //jsfiddle.net/tTX6E/


if-else가없는 더 나은 솔루션 :

$(document).ready(function() {
    $("#chkdwn2").click(function() {
        $("#dropdown").prop("disabled", this.checked);  
    });
});

$("#chkdwn2").change(function() { 
    if (this.checked) $("#dropdown").prop("disabled",'disabled');
}) 

$(document).ready(function() {
 $('#chkdwn2').click(function() {
   if ($('#chkdwn2').prop('checked')) {
      $('#dropdown').prop('disabled', true);
   } else {
      $('#dropdown').prop('disabled', false);  
   }
 });
});

성명 .prop에서 사용 if.

참고 URL : https://stackoverflow.com/questions/7703241/enable-disable-a-dropdownbox-in-jquery

반응형