투케이2K

75. (jquery/제이쿼리) change , each , checked 사용해 실시간 라디오 (radio) 버튼 클릭 변경 상태 확인 본문

Jquery

75. (jquery/제이쿼리) change , each , checked 사용해 실시간 라디오 (radio) 버튼 클릭 변경 상태 확인

투케이2K 2022. 12. 2. 16:14
반응형

[개발 환경 설정]

개발 툴 : Edit++

개발 언어 : jquery

 

[소스 코드]

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





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


        /**
         * --------------------------------
         * [요약 설명]
         * --------------------------------
         * 1. load : html 최초 로드 수행 시 호출 되는 함수입니다
         * --------------------------------
         * 2. change : 요소 변경 상태를 감지 합니다
         * --------------------------------
         * 3. 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(""); 


            // [라디오 버튼 change , each 사용해 실시간 변경 된 값 확인 실시 / name = rdNumbers]
            $('input[name="rdNumbers"]').change(function() {

                // [모든 radio 순회]
                $('input[name="rdNumbers"]').each(function() {

                    var value = $(this).val(); // value
                    var checked = $(this).prop('checked'); // jQuery 1.6 이상 (jQuery 1.6 미만에는 prop()가 없음)
             
                    if(checked == true) { // 체크 된 경우
                        console.log("");
                        console.log("=========================================");
                        console.log("[testMain] : [라디오 버튼 체크 상태 확인]");
                        console.log("[value] : " + value);
                        console.log("[checked] : " + checked);
                        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