▼選択した項目(option要素)を2つのselect要素間で移動(追加・削除)します。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="ja" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="imagetoolbar" content="no" /> <title>DOM制御・更新処理:内部にコンテンツを挿入:appendTo()の使用例 | jQuery</title> <link rel="stylesheet" type="text/css" href="/content/lib/global.css" /> <!-- JS --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $("#btn_add").click(function(){ $("#sel1 option:selected").remove().appendTo("#sel2"); }); $("#btn_remove").click(function(){ $("#sel2 option:selected").remove().appendTo("#sel1"); }); }); </script> <style type="text/css"> .demo { width:24em; } .demo p { margin:1em 0; text-align:center; } .cleft { width:12em; float:left; } .cright { width:12em; float:right; } select { width:10em; height:10em; } .demo a { border:1px solid #ccc; padding:2px 10px; background-color:#fff; text-decoration:none; } .demo a:hover { background-color:yellow; } </style> <link rel="stylesheet" type="text/css" href="/common/css/example.css"></head> <body id='example3' class='example'><div class="ads" style="margin:32px auto;text-align:center;"></div><h1 class='h'><a href='/'>PHP & JavaScript Room</a> :: 設置サンプル</h1> <h3 class='h'>実行結果</h3> <div id="wrap"> <h1>DOM制御・更新処理:内部にコンテンツを挿入:appendTo()の使用例 | jQuery</h1> <h2>例:複数選択型プルダウンメニューの項目の追加と削除</h2> <p>▼選択した項目(option要素)を2つのselect要素間で移動(追加・削除)します。</p> <!-- CODE --> <div class="demo cf"> <div class="cleft"> <select multiple id="sel1"> <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> <p><a href="#" id="btn_add">追加 »</a></p> </div> <div class="cright"> <select multiple id="sel2"></select> <p><a href="#" id="btn_remove">« 削除</a></p> </div> </div> <!-- CODE / --> </div> </body> </html>