0

我通过以下方式向选择菜单添加新选项:

var options1 = {
    val1 : 'text1',
    val2 : 'text2'
};
$.each(options1, function(val, text) {
    $('#SelectMenu').append( new Option(text,val) );
});

我们如何将 opt 组添加到选择菜单中?

4

1 回答 1

2

工作示例:http: //jsfiddle.net/rULKH/28/

JS

var groups = {
    1 : 'USPS',
    2 : 'FedEx'
};

var optionsGroup1 = {
    standard  : 'Standard: 7 day',
    rush      : 'Rush: 3 days',
    express   : 'Express: next day',
    overnight : 'Overnight'
}

var optionsGroup2 = {
    firstOvernight : 'First Overnight',
    expressSaver   : 'Express Saver',
    ground         : 'Ground'
}

$.each(groups, function(groupIndex, groupText) { 
    $('#shipping-options').append('<optgroup label="'+groupText+'" id="'+groupText+'" />');

    $.each(eval('optionsGroup' + groupIndex), function (optionText, optionIndex) {
        $('#'+groupText).append(new Option(optionIndex,optionText));
    });
});

$('#shipping-options').selectmenu("refresh");
$('#option-page').page();

HTML

<div data-role="page" data-theme="b" id="option-page"> 
    <div data-role="content"> 

       <div data-role="fieldcontain"> 
            <label for="shipping-options" class="select">Choose shipping method:</label> 
            <select name="shipping-options" id="shipping-options" data-native-menu="false"> 

            </select> 
        </div> 

    </div>
</div>
于 2011-06-16T14:54:52.953 回答