복붙노트

[JQUERY] 어떻게 라디오 버튼의 선택을 취소합니다?

JQUERY

어떻게 라디오 버튼의 선택을 취소합니다?

해결법


  1. 1.중 (일반 JS)

    중 (일반 JS)

    this.checked = false;
    

    또는 (jQuery를)

    $(this).prop('checked', false);
    // Note that the pre-jQuery 1.6 idiom was
    // $(this).attr('checked', false);
    

    ) (ATTR ()와 지주 () 이유 소품의 차이에 대한 설명은 jQuery를 소품 () 도움말 페이지를 참조하십시오 지금 바람직하다. 소품 ()는 2011 년 5 월 jQuery를 1.6에서 도입되었습니다.


  2. 2.당신은 각각의 기능이 필요하지 않을

    당신은 각각의 기능이 필요하지 않을

    $("input:radio").attr("checked", false);
    

    또는

    $("input:radio").removeAttr("checked");
    

    동일은 또한 당신의 텍스트 상자에 적용해야합니다 :

    $('#frm input[type="text"]').val("");
    

    하지만 당신은이를 향상시킬 수

    $('#frm input:text').val("");
    

  3. 3.시험

    시험

    $(this).removeAttr('checked')
    

    브라우저의 많은 해석하는 것이기 때문에 '선택 = 아무것도'사실로. 이 모두 확인 된 속성을 제거합니다.

    도움이 되었기를 바랍니다.


  4. 4.이고르의 코드를 기반으로 Laurynas '플러그인의 약간의 수정. 이 라디오 버튼이 표적이되고와 관련된 가능한 라벨을 수용 :

    이고르의 코드를 기반으로 Laurynas '플러그인의 약간의 수정. 이 라디오 버튼이 표적이되고와 관련된 가능한 라벨을 수용 :

    (function ($) {
        $.fn.uncheckableRadio = function () {
    
            return this.each(function () {
                var radio = this;
                    $('label[for="' + radio.id + '"]').add(radio).mousedown(function () {
                        $(radio).data('wasChecked', radio.checked);
                    });
    
                    $('label[for="' + radio.id + '"]').add(radio).click(function () {
                        if ($(radio).data('wasChecked'))
                            radio.checked = false;
                    });
               });
        };
    })(jQuery);
    

  5. 5.감사합니다 패트릭, 내 일했다! 그것은 당신이 사용해야 mousedown입니다. 당신이 라디오 버튼 그룹을 처리 할 수 ​​있도록 그러나 나는 코드를 개선했습니다.

    감사합니다 패트릭, 내 일했다! 그것은 당신이 사용해야 mousedown입니다. 당신이 라디오 버튼 그룹을 처리 할 수 ​​있도록 그러나 나는 코드를 개선했습니다.

    //We need to bind click handler as well
    //as FF sets button checked after mousedown, but before click
    $('input:radio').bind('click mousedown', (function() {
        //Capture radio button status within its handler scope,
        //so we do not use any global vars and every radio button keeps its own status.
        //This required to uncheck them later.
        //We need to store status separately as browser updates checked status before click handler called,
        //so radio button will always be checked.
        var isChecked;
    
        return function(event) {
            //console.log(event.type + ": " + this.checked);
    
            if(event.type == 'click') {
                //console.log(isChecked);
    
                if(isChecked) {
                    //Uncheck and update status
                    isChecked = this.checked = false;
                } else {
                    //Update status
                    //Browser will check the button by itself
                    isChecked = true;
    
                    //Do something else if radio button selected
                    /*
                    if(this.value == 'somevalue') {
                        doSomething();
                    } else {
                        doSomethingElse();
                    }
                    */
                }
        } else {
            //Get the right status before browser sets it
            //We need to use onmousedown event here, as it is the only cross-browser compatible event for radio buttons
            isChecked = this.checked;
        }
    }})());
    

  6. 6.플러그인 등의 이고르의 코드로 다시 작성합니다.

    플러그인 등의 이고르의 코드로 다시 작성합니다.

    사용하다:

    $('input[type=radio]').uncheckableRadio();
    

    플러그인:

    (function( $ ){
    
        $.fn.uncheckableRadio = function() {
    
            return this.each(function() {
                $(this).mousedown(function() {
                    $(this).data('wasChecked', this.checked);
                });
    
                $(this).click(function() {
                    if ($(this).data('wasChecked'))
                        this.checked = false;
                });
            });
    
        };
    
    })( jQuery );
    

  7. 7.라디오와 라디오 그룹 :

    라디오와 라디오 그룹 :

    $(document).ready(function() {
        $(document).find("input:checked[type='radio']").addClass('bounce');   
        $("input[type='radio']").click(function() {
            $(this).prop('checked', false);
            $(this).toggleClass('bounce');
    
            if( $(this).hasClass('bounce') ) {
                $(this).prop('checked', true);
                $(document).find("input:not(:checked)[type='radio']").removeClass('bounce');
            }
        });
    });
    

  8. 8.이보십시오,이 트릭을 할 것입니다 :

    이보십시오,이 트릭을 할 것입니다 :

            $(document).ready(function() {
               $("input[type='radio']").mousedown(function(e) {
                    if ($(this).attr("checked") == true) {
                       setTimeout("$('input[id=" + $(this).attr('id') + "]').removeAttr('checked');", 200);}
                    else {
                        return true
                    }
                });
            });
    

  9. 9.시험

    시험

    $(this).attr("checked" , false );
    

  10. 10.또한 전용 체크 박스를 사용하여 라디오 버튼의 동작을 자극 할 수있다 :

    또한 전용 체크 박스를 사용하여 라디오 버튼의 동작을 자극 할 수있다 :

    <input type="checkbox" class="fakeRadio" checked />
    <input type="checkbox" class="fakeRadio" />
    <input type="checkbox" class="fakeRadio" />
    

    그렇다면, 당신은 당신을 위해 작동하는 간단한 코드를 사용할 수 있습니다 :

    $(".fakeRadio").click(function(){
        $(".fakeRadio").prop( "checked", false );
        $(this).prop( "checked", true );
    });
    

    그것은 잘 작동하고 각 버튼의 동작을 더 잘 제어 할 수 있습니다.

    http://jsfiddle.net/almircampos/n1zvrs0c/ : 당신은 스스로 그것을 시도 할 수 있습니다

    이 포크도 보자는 의견에의 요청에 따라 모든 선택을 취소입니다 : http://jsfiddle.net/5ry8n4f4/


  11. 11.그냥 jQuery를 위해 다음과 같은 코드를 넣어 :

    그냥 jQuery를 위해 다음과 같은 코드를 넣어 :

    jQuery("input:radio").removeAttr("checked");
    

    그리고 자바 스크립트에 대한 :

    $("input:radio").removeAttr("checked");
    

    어떤 foreach 루프, .each () 함수 또는 어떤 일을 넣을 필요가 없습니다


  12. 12.이것을 사용

    이것을 사용

    $("input[name='nameOfYourRadioButton']").attr("checked", false);
    

  13. 13.

    $('#frm input[type="radio":checked]').each(function(){
       $(this).checked = false;  
      });
    

    이 거의 좋은 그러나 당신은 [0]을 놓친

    수정 - >> $ (이) [0] .checked를 = 거짓;


  14. 14.

    function setRadio(obj) 
    {
        if($("input[name='r_"+obj.value+"']").val() == 0 ){
          obj.checked = true
         $("input[name='r_"+obj.value+"']").val(1);
        }else{
          obj.checked = false;
          $("input[name='r_"+obj.value+"']").val(0);
        }
    
    }
    
    <input type="radio" id="planoT" name="planoT[{ID_PLANO}]" value="{ID_PLANO}" onclick="setRadio(this)" > <input type="hidden" id="r_{ID_PLANO}" name="r_{ID_PLANO}" value="0" >
    

    :디


  15. 15.당신은 선택을 취소 라디오 버튼이 JQuery와 사용할 수 있습니다

    당신은 선택을 취소 라디오 버튼이 JQuery와 사용할 수 있습니다

    $('input:radio[name="IntroducerType"]').removeAttr('checked');
                    $('input:radio[name="IntroducerType"]').prop('checked', false);
    

  16. 16.

    $('input[id^="rad"]').dblclick(function(){
        var nombre = $(this).attr('id');
        var checked =  $(this).is(":checked") ;
        if(checked){
            $("input[id="+nombre+"]:radio").prop( "checked", false );
        }
    });
    

    때마다 당신은 체크 라디오에서 더블 클릭을 false로 확인 변경

    나는이 ID 선택기를 사용하기 때문에 내 라디오 ID = radxxxxxxxx로 시작합니다.


  17. 17.

    function clearForm(){
      $('#frm input[type="text"]').each(function(){
          $(this).val("");  
      });
      $('#frm input[type="radio":checked]').each(function(){
          $(this).attr('checked', false);  
      });
     }
    

    올바른 선택은 : #frm 입력 [TYPE = "라디오"] : 체크 하지 #frm 입력 [TYPE = "라디오"체크]

  18. from https://stackoverflow.com/questions/2117538/how-to-uncheck-a-radio-button by cc-by-sa and MIT license