jquery mobile:多选下拉列表

时间:2011-10-03 06:45:59

标签: jquery jquery-mobile

我的jquerymobile表单有一个optgrouped多部分下拉列表。在加载表单时,我必须设置一些默认选项。我试过像

$('#myselect')[0].selectedIndex = 2;
$('#myselect')[0].selectedIndex = 3;
$('#myselect')[0].selectedIndex = 5;
$('#myselect').selectmenu("refresh");

但是只选择了第5个选项,如何使用js为多个选项设置所选选项为true。

+++ EDIT ++++

<!DOCTYPE html>
<link rel="stylesheet" href="jquery.mobile-1.0b3/jquery.mobile-1.0b3.min.css" />        
  <script src="jquery.mobile-1.0b3/jquery-1.6.2.min.js"></script> 
  <script type="text/javascript">
        $("#account").live("pagecreate", function(event){
            if( navigator.userAgent.match(/android/i) ) {
                loadOrgDropdown();
            }           
        });

 </script>

<script src="jquery.mobile-1.0b3/jquery.mobile-1.0b3.min.js"></script> 
<script src="js/common.js"></script> 
<script type="text/javascript">


        function loadDropdown() { 
            jQuery.ajax(
                    {
            url         : 'PocketmediServices.php?service=getDropdownValues',
            type        : 'get',
            dataType    : 'json',
            beforeSend  : function(){
            },
            error       : function(){
                        if (typeof Android != 'undefined') {
                            Android.dismissLoadinDialog();
                        }
            },
            success     : function(data){
              if(data.response.getDropdownValues.dropdownList  !== undefined) {
                var count = -1;
                var selected = -1;
                $.each(data.response.getDropdownValues.dropdownList, function(key, val) {   
                    count++;
                    $('#myselect')
                    .append($('<option>', { value : val.item.Code })
                    .text(val.item.Name));  
                    if($.inArray(val.item.optionval, previousSelected) >=0) {
                        $('#myselect')[0].selectedIndex = [3,5]; // hard coded for now.
                    }
                }); 
                $('#myselect').selectmenu("refresh");
              }     
            },
            complete    : function(req){
            },
           });  

        } //loadDropdown

    </script>

    <div data-role="fieldcontain">
        <label for="organization">Organization:</label> 
        <select name="organization" id="organization" data-native-menu="false" multiple >
            <optgroup label="s" id="org_s"></optgroup>
            <optgroup label="Support Networks" id="org_support_networks"></optgroup>
       </select>
    </div>

谢谢, nehatha

1 个答案:

答案 0 :(得分:0)

多个选择框处理多个值的数组,例如

$('#myselect').val([2,3,5]);

您可以找到示例@ http://api.jquery.com/val/