투케이2K

79. (jquery/제이쿼리) text , val 사용해 select box 셀렉트 박스 텍스트 값 및 value 값 확인 실시 본문

Jquery

79. (jquery/제이쿼리) text , val 사용해 select box 셀렉트 박스 텍스트 값 및 value 값 확인 실시

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

[개발 환경 설정]

개발 툴 : Edit++

개발 언어 : jquery

 

[소스 코드]

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





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


        /**
         * --------------------------------
         * [요약 설명]
         * --------------------------------
         * 1. load : html 최초 로드 수행 시 호출 되는 함수입니다
         * --------------------------------
        */



        // [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] : [셀렉트 박스 text 값 확인 / id 값 기준]
            var text = $('#selectAddrs option:selected').text();


            // [2] : [셀렉트 박스 value 값 확인 / id 값 기준]
            var value = $('#selectAddrs').val();


            // [3] : [로그 출력]
            console.log("");
            console.log("=========================================");
            console.log("[testMain] : [셀렉트 박스 정보 확인]");
            console.log("[text] : " + text);
            console.log("[value] : " + value);
            console.log("=========================================");
            console.log("");
        };


    </script>
​

<body>


    <!-- [select 선언 실시] -->
    <select id="selectAddrs" name="selectAddrs">
        <optgroup label="- 경상도">
            <option value="yeongju" selected>영주</option>
            <option value="gumi">구미</option>
            <option value="andong">안동</option>
        </optgroup>
        <optgroup label="- 서울">
            <option value="gasan">가산</option>
            <option value="doksan">독산</option>
            <option value="guro">구로</option>
        </optgroup>
        <option value="jejudo">제주도</option>
    </select>


</body>
 

[결과 출력]

 

 

반응형
Comments