투케이2K

76. (jquery/제이쿼리) prop checked , each not checked 사용해 라디오 (radio) 버튼 체크 상태 확인 실시 - check, unChecke 본문

Jquery

76. (jquery/제이쿼리) prop checked , each not checked 사용해 라디오 (radio) 버튼 체크 상태 확인 실시 - check, unChecke

투케이2K 2022. 12. 2. 17:02
반응형

[개발 환경 설정]

개발 툴 : Edit++

개발 언어 : jquery

 

[소스 코드]

    <!-- [CDN 라이브러리 설치] -->
    <script src="https://code.jquery.com/jquery-latest.min.js"></script>





    <!-- [내부 자바스크립트 J쿼리 이벤트 지정] -->
    <script>


        /**
         * --------------------------------
         * [요약 설명]
         * --------------------------------
         * 1. load : html 최초 로드 수행 시 호출 되는 함수입니다
         * --------------------------------
         * 2. each : 반복문을 수행하며 요소 상태를 확인 합니다
         * --------------------------------
        */



        // [html 최초 로드 및 이벤트 상시 대기 실시]
        $(window).load(function(){
            console.log("");
            console.log("=========================================");
            console.log("[window load] : [html 로드 수행 실시]");
            console.log("=========================================");
            console.log("");

            // [테스트 함수 호출]
            testMain();
        });



        // [테스트 자바스크립트 함수]
        function testMain(){
            console.log("");
            console.log("=========================================");
            console.log("[testMain] : [start]");
            console.log("=========================================");
            console.log(""); 


            // [1] : [라디오 버튼 전체 체크 해제 / name = rdNumbers]
            $('input[name="rdNumbers"]').each(function() {

                // [라디오 버튼 전체 unChecked 수행]
                $(this).prop('checked', false); // jQuery 1.6 이상 (jQuery 1.6 미만에는 prop()가 없음)
            });



            // [2] : prop 를 사용해 특정 id 값을 가지는 라디오 버튼 체크 설정
            $('#rd_3').prop('checked', true);



            // [3] : 체크된 라디오 버튼 id, value 값 확인
            var id = $('input[name="rdNumbers"]:checked').attr('id');
            var value = $('input[name="rdNumbers"]:checked').val();

            console.log("");
            console.log("=========================================");
            console.log("[testMain] : [라디오 버튼 체크 정보 확인]");
            console.log("[id] : " + id);
            console.log("[value] : " + value);
            console.log("=========================================");
            console.log(""); 



            // [4] : 체크 되지 않은 라디오 값 확인
            var array = []

            $('input[name="rdNumbers"]:not(:checked)').each(function() {

                var id_data = $(this).attr('id'); // 아이디 값
                var value_data = $(this).val(); // value

                var format = {id : String(id_data), value : String(value_data)}; // 정보 포맷

                array.push(format); // 배열에 삽입
            });

            console.log("");
            console.log("=========================================");
            console.log("[testMain] : [라디오 버튼 체크 되지 않은 데이터 확인]");
            console.log("[array] : " + JSON.stringify(array));
            console.log("=========================================");
            console.log(""); 
        };


    </script>
​

<body>


    <!-- [radio 선언 실시] -->
    <fieldset>
        <legend>Numbers</legend>
        <input type="radio" id="rd_1" name="rdNumbers" value="1" checked><label for="rd_1">1</label>
        <input type="radio" id="rd_2" name="rdNumbers" value="2"><label for="rd_2">2</label>
        <input type="radio" id="rd_3" name="rdNumbers" value="3"><label for="rd_3">3</label>
        <input type="radio" id="rd_4" name="rdNumbers" value="4"><label for="rd_4">4</label>
    </fieldset>


</body>
 

[결과 출력]

 

 

반응형
Comments