본문 바로가기
Develop/JavaScript & JQuery

SELECT BOX(1차)

by jaekk 2018. 6. 11.
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="http://code.jquery.com/jquery-1.11.3.js"></script>
</head>
<body>
    <select id ="box1" name="box1"size=4 multiple>
        <option value="baseBall" name="sport_box1" >야구</option>
        <option value="basketBall" name="sport_box1">농구</option>
        <option value="soccer" name="sport_box1">축구</option>
        <option value="hockey" name="sport_box1">하키</option>
    </select>
    <button name="btn"><<</button>
    <button name="btn"><</button>
    <button name="btn">></button>
    <button name="btn">>></button>
 
    <select id ="box2" size=4 multiple>
    </select>
 
    <script>
    $(document).ready(function(){
        var s1 = $('#box1');
        var s2 = $('#box2');
        var s3 = $('#box1[name=sport_box1]');
        var s4 = $('#box2');
 
        // var btn = $('button[name=btn]');
        // 버튼 이벤트 추가
        
        $('button[name=btn]:eq(0)').bind('click',function(){
            $('#box2 option').remove();
        });
        $('button[name=btn]:eq(1)').bind('click',function(){
           $('#box2 option:selected').remove();
                      
        });
        $('button[name=btn]:eq(2)').bind('click',function(){
            var select = $('#box1 option:selected');
            for(var i=0;i<select.length;i++){
                var option = $('<option value="'+select[i].value+' name="sport_box2">'+select[i].text+'</option>');
                $('#box2').append(option);
            }
        });
        $('button[name=btn]:eq(3)').bind('click',function(){
            var select = $('#box1 option');
            for(var i=0;i<select.length;i++){
                var option = $('<option value="'+select[i].value+' name="sport_box2">'+select[i].text+'</option>');
                $('#box2').append(option);
            }
        });
 
    });
    </script>
</body>
</html>
cs

 

'Develop > JavaScript & JQuery' 카테고리의 다른 글

자료형  (0) 2018.06.26
함수  (0) 2018.06.26
SELECT BOX(2차)  (0) 2018.06.11
jQuery 이벤트  (0) 2018.06.10
JQuery  (0) 2018.06.10

댓글