使用 jQuery 填充“从集合中选择框”

Populating Select Box from Collection using jQuery

本文关键字:选择 集合 jQuery 填充 使用      更新时间:2023-09-26

我对jQuery很陌生,我正在尝试从集合中填充一个选择框。 基本上,我想采用下面的代码,其中"statuses"是集合并将其移动到jQuery:

<widget:select property="statusCode" styleId="STATUSCODELIST" labelKey="label.status">
    <html:option value=""/>
    <c:forEach var="status" items="${applicationScope.statuses}">
        <html:option value="${status.key}"><c:out value="${status.key} - ${status.value}"/></html:option>
    </c:forEach>
</widget:select>

我正在尝试弄清楚如何让"状态"像此示例中的数字数组一样工作:

var numbers  = [ 1, 2, 3, 4, 5]
var list = $('#STATUSCODELIST')[0];
$.each(numbers, function(index, text) { 
    list.options[list.options.length] = new Option(index, text);
});

感谢您的任何帮助。

假设您有一个 id 为"test_DDL"的选择框,您可以执行以下操作:

var statusList = $('#test_DDL');
$.each(statuses, function (idx, status) {
    var option = "<option>" + status.key + " - " + status.value + "</option>";
    statusList.append(option);
});