메뉴 건너뛰기

조회 수 1211 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

저번에 맹글었던거에서 옵션을 추가하는 기능을 추가해보았다.


이 예제에서 꽤 많은 jQuery 셀렉터와 함수들을 사용한것 같은데 소스를 이해해 보면서 jQuery 사용법을 살살 익혀봐도 좋을것 같다.




만들어질 것은 다음과 같다. (직접 클릭해 보기바람)
옵션명 항목명 필수항목 가격 재고 옵션추가

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
     
    <script type="text/javascript">
        $(document).ready(function(){
            // 옵션추가 버튼 클릭시
            $("#addItemBtn").click(function(){
                // item 의 최대번호 구하기
                var lastItemNo = $("#example tr:last").attr("class").replace("item", "");
 
                var newitem = $("#example tr:eq(1)").clone();
                newitem.removeClass();
                newitem.find("td:eq(0)").attr("rowspan", "1");
                newitem.addClass("item"+(parseInt(lastItemNo)+1));
 
                $("#example").append(newitem);
            });
 
 
            // 항목추가 버튼 클릭시
            $(".addBtn").live("click", function(){
                var clickedRow = $(this).parent().parent();
                var cls = clickedRow.attr("class");
 
                // tr 복사해서 마지막에 추가
                var newrow = clickedRow.clone();
                newrow.find("td:eq(0)").remove();
                newrow.insertAfter($("#example ."+cls+":last"));
 
                // rowspan 조정
                resizeRowspan(cls);
            });
             
             
            // 삭제버튼 클릭시
            $(".delBtn").live("click", function(){
                var clickedRow = $(this).parent().parent();
                var cls = clickedRow.attr("class");
                 
                // 각 항목의 첫번째 row를 삭제한 경우 다음 row에 td 하나를 추가해 준다.
                if( clickedRow.find("td:eq(0)").attr("rowspan") ){
                    if( clickedRow.next().hasClass(cls) ){
                        clickedRow.next().prepend(clickedRow.find("td:eq(0)"));
                    }
                }
 
                clickedRow.remove();
 
                // rowspan 조정
                resizeRowspan(cls);
            });
 
            // cls : rowspan 을 조정할 class ex) item1, item2, ...
            function resizeRowspan(cls){
                var rowspan = $("."+cls).length;
                $("."+cls+":first td:eq(0)").attr("rowspan", rowspan);
            }
        });
    </script>
</head>
 
<body>
<button id="addItemBtn">옵션추가</button>
<table id="example" border="1px">
        <tr>
            <th>옵션명</th>
            <th>항목명</th>
            <th>필수항목</th>
            <th>가격</th>
            <th>재고</th>
            <th>옵션추가</th>
        </tr>
        <tr class="item1">
            <td><input type="text" /><button class="addBtn">항목추가</button></td>
            <td><input type="text" /></td>
            <td><input type="checkbox" /></td>
            <td><input type="text" /></td>
            <td><input type="text" /></td>
            <td><button class="delBtn">삭제</button></td>
        </tr>
        <tr class="item2">
            <td><input type="text" /><button class="addBtn">항목추가</button></td>
            <td><input type="text" /></td>
            <td><input type="checkbox" /></td>
            <td><input type="text" /></td>
            <td><input type="text" /></td>
            <td><button class="delBtn">삭제</button></td>
        </tr>
        <tr class="item3">
            <td><input type="text" /><button class="addBtn">항목추가</button></td>
            <td><input type="text" /></td>
            <td><input type="checkbox" /></td>
            <td><input type="text" /></td>
            <td><input type="text" /></td>
            <td><button class="delBtn">삭제</button></td>
        </tr>
        <tr class="item4">
            <td><input type="text" /><button class="addBtn">항목추가</button></td>
            <td><input type="text" /></td>
            <td><input type="checkbox" /></td>
            <td><input type="text" /></td>
            <td><input type="text" /></td>
            <td><button class="delBtn">삭제</button></td>
        </tr>
</table>
</body>
</html> 


List of Articles
번호 제목 날짜 조회 수
179 jquery timer, javascript countdown (타이머 예제) 2018.11.07 81451
178 jquery 플러그인 링크 모음 2016.12.22 49605
177 동적으로 콤보(select)의 항목(option) 생성하기 2016.09.21 14374
176 이미지 클릭시 확대하기 file 2016.09.21 12643
175 jqGrid / custom pager / custom paging / 그리드 / 페이징 file 2016.09.09 12507
174 Ajax를 통해 전송된 데이터를 Controller에서 List 객체로 받기 2021.03.25 12227
173 jQuery 입문: form 데이터 유효성 검사 2017.03.02 11799
172 속성선택자를 이용해서 링크에 스타일 입히기 2016.09.21 11535
171 Ajax를 이용한 데이터 조회시 로딩 로딩 이미지 보이기(jquery이용) file 2016.12.22 10746
170 jquery 이용한 필수입력체크(input form) 2017.03.02 10558
169 셀렉트박스에서 특정 옵션 선택시 레이어 나타내기 2016.12.22 10484
168 bxslider 멈춤현상 2016.12.22 10110
167 jQuery 날짜 비교 (날짜비교) 2016.11.17 10055
166 jquery radio checked (라디오 버튼 값으로 선택), 검색 조건 초기 셋팅 2016.11.17 10043
165 각종 다이어그램 플러그인 / 차트 / 관계 플러그인 2016.09.09 9487
164 POST 방식으로 인수를 전달하고 그 결과를 받아오기 2016.09.21 9202
163 select box 값 변경, 목록 변경 2016.09.21 9065
162 파일 업로드 방법, 이미지 파일 업로드 예제 소스 2017.03.06 9028
161 [jQuery] 이용 아이디 중복체크 실시간 2016.09.11 8876
160 jquery 글자 byte, 문자열 byte(바이트) 길이 체크하기 예제 2016.11.17 8835
Board Pagination Prev 1 2 3 4 5 6 7 8 9 Next
/ 9

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved