반응형
출처
http://blog.jeremymartin.name/2008/02/easy-multi-select-transfer-with-jquery.html
[js]
- $().ready(function() {
- $('#add').click(function() {
- return !$('#select1 option:selected').remove().appendTo('#select2');
- });
- $('#remove').click(function() {
- return !$('#select2 option:selected').remove().appendTo('#select1');
- });
- });
[html]
- <html>
- <head>
- <script src="js/jquery.js" type="text/javascript"></script>
- <script type="text/javascript">
- $().ready(function() {
- $('#add').click(function() {
- return !$('#select1 option:selected').remove().appendTo('#select2');
- });
- $('#remove').click(function() {
- return !$('#select2 option:selected').remove().appendTo('#select1');
- });
- });
- </script>
- <style type="text/css">
- a {
- display: block;
- border: 1px solid #aaa;
- text-decoration: none;
- background-color: #fafafa;
- color: #123456;
- margin: 2px;
- clear:both;
- }
- div {
- float:left;
- text-align: center;
- margin: 10px;
- }
- select {
- width: 100px;
- height: 80px;
- }
- </style>
- </head>
- <body>
- <div>
- <select multiple id="select1">
- <option value="1">Option 1</option>
- <option value="2">Option 2</option>
- <option value="3">Option 3</option>
- <option value="4">Option 4</option>
- </select>
- <a href="#" id="add">add >></a>
- </div>
- <div>
- <select multiple id="select2"></select>
- <a href="#" id="remove"><< remove</a>
- </div>
- </body>
- </html>
As was mentioned in the comments below, the following (slightly modified) code can be used to automatically select all options in the second select box before submitting (thanks Charlie!).
- $('form').submit(function() {
- $('#select2 option').each(function(i) {
- $(this).attr("selected", "selected");
- });
- });
반응형
'Programming Language > Javascript' 카테고리의 다른 글
[Javascript] Form 데이터 문자열로 변환 시, serialize() 사용하기 (0) | 2017.10.12 |
---|---|
(스크랩) 프로토타입 이해하기 (0) | 2017.09.19 |
자바스크립트 한 줄 압축풀기 사이트 (0) | 2017.09.14 |
multiple selected option 의 값 추출, jQuery (0) | 2017.09.12 |
변수와 프로퍼티 (0) | 2017.07.26 |