1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE HTML>
<html lang="ko">
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="lib/jQuery/2.2.3/jquery.min.js"></script>
</head>
<body>
 
    <select name="test">
        <option value="테스트1" selected>테스트1</option>
        <option value="테스트2">테스트2</option>
        <option value="테스트3">테스트3</option>
        <option value="테스트4" >테스트4</option>
        <option value="테스트5">테스트5</option>
    </select>
    <script>
 
        // option value로 선택
        $("select").val("테스트2");
        //또는 $("select[name='test'] option[value='테스트3']").attr("selected", "selected");
        
        // option 텍스트로 선택
        //$("select[name='test'] option:contains('테스트5')").attr("selected", "selected");
 
        // 또는 (크롬에서 attr메서드가 적용안되어.. prop로 변경함 )
        // $("select option:eq(0)").prop("selected", true);
 
        // 선택된 select option값 얻기
        console.log($("select").val());
              
    </script>
 
</body>
</html>


반응형
Posted by 힘없는염소