JQuery Multi Select With Two ListBox

蒯嘉赐
2023-12-01

jQuery 的寫法如下:

<script type="text/javascript" language="javascript"> 
 $().ready(function () {
         $("input[name^='add']").click(function () {
             var id = $(this).attr('id').split('_');
             if (id[1] == 'all') {
                 $('#select1 option').remove().appendTo('#select2');
             } else {
                 $('#select1 option:selected').remove().appendTo('#select2');
             }
             var Mult_SelectListBox = $('#select2 option').map(function () { return $(this).val(); }).get().join(',');
             $("#Mult_SelectListBox").attr('value', Mult_SelectListBox);
             return false;
         });
         $("input[name^='remove']").click(function () {
             var id = $(this).attr('id').split('_');
             if (id[1] == 'all') {
                 $('#select2 option').remove().appendTo('#select1');
             } else {
                 $('#select2 option:selected').remove().appendTo('#select1');
             }
             var Mult_SelectListBox = $('#select2 option').map(function () { return $(this).val(); }).get().join(',');
             $("#Mult_SelectListBox").attr('value', Mult_SelectListBox);
             return false;
         });
     });  
 </script>

The Working Test Page 如下:

<html>  
<head>  
 <style type="text/css">  
  td.selectbox {  
   float:left;  
   text-align: center;  
   margin: 10px;  
  }  
  select {  
   width: 100px;  
   height: 80px;  
  }  
 </style>  
</head>  
<body> 
<table>
        <tr>
            <td class="selectbox">
                <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>
                    <option value="5">Option 5</option>
                    <option value="6">Option 6</option>
                    <option value="7">Option 7</option>
                    <option value="8">Option 8</option>
                </select>
            </td>
            <td class="selectbox">
                <input type="button" id="add_all" name="add" value="&gt;&gt;" style="width: 60px" /><br />
                <input type="button" id="remove_all" name="remove" value="&lt;&lt;" style="width: 60px" /><br />
                <input type="button" id="add_one" name="add_one" value="&gt;" style="width: 60px" /><br />
                <input type="button" id="remove_one" name="remove_one" value="&lt;" style="width: 60px" /><br />
                <input type="hidden" id="Mult_SelectListBox" name="Mult_SelectListBox" value="" /><!—-Request hidden Value -->
            </td>
            <td class="selectbox">
                <select multiple id="select2">
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <input type="button" οnclick="submitQuery()" value="submit" />
            </td>
            <td>
            </td>
        </tr>
    </table>
</body>  
</html>

 

Get value to alert():

     function submitQuery() {
         var value = $("#Mult_SelectListBox").attr('value');
         alert("select2 Value: "+value);
     }
 类似资料:

相关阅读

相关文章

相关问答