将jsonList解析为dropdownlist Jquery时出错

将jsonList解析为dropdownlist Jquery时出错,jquery,json,drop-down-menu,Jquery,Json,Drop Down Menu,我在用JSON列表填充下拉列表时遇到问题。 以下是我在我的功能中所做的 onPhaseChange1: function(dropdown, row) { var combobox = $(dropdown); comboboxWorkUnit = row.find("select.workUnit"); EmployeeType = $("input[id*='EmployeeType']").val(); comboboxW

我在用JSON列表填充下拉列表时遇到问题。 以下是我在我的功能中所做的

onPhaseChange1: function(dropdown, row) {

        var combobox = $(dropdown);
        comboboxWorkUnit = row.find("select.workUnit");
        EmployeeType = $("input[id*='EmployeeType']").val();

        comboboxWorkUnit.show();
        comboboxWorkUnit.empty();

        var jsonList = {
        [{ "Id": "12345", "WorkUnitId": "SR0001954", "Description": "Test Service Request From Serena", "WorkUnitCategory": "ServiceRequest" },
{ "Id": "12355", "WorkUnitId": "WOR001854", "Description": "Test Work Order From Serena", "WorkUnitCategory": "ServiceRequest" },
{ "Id": "12365", "WorkUnitId": "DBR001274", "Description": "Test Database Related Service Request From Serena", "WorkUnitCategory": "ServiceRequest"}]}


$($.parseJSON(jsonList)).map(function() { $('<option>').val(this.Id).text(this.Id).appendTo(comboboxWorkUnit); }); 
    },
onPhaseChange1:函数(下拉列表,第行){
变量组合框=$(下拉菜单);
comboboxWorkUnit=row.find(“select.workUnit”);
EmployeeType=$(“输入[id*='EmployeeType']”)val();
comboboxWorkUnit.show();
comboboxWorkUnit.empty();
var jsonList={
[{“Id”:“12345”,“WorkUnitId”:“SR0001954”,“Description”:“来自Serena的测试服务请求”,“WorkUnitCategory”:“ServiceRequest”},
{“Id”:“12355”,“WorkUnitId”:“WOR001854”,“Description”:“来自Serena的测试工作订单”,“WorkUnitCategory”:“ServiceRequest”},
{“Id”:“12365”,“WorkUnitId”:“DBR001274”,“Description”:“来自Serena的测试数据库相关服务请求”,“WorkUnitCategory”:“ServiceRequest”}]}
$($.parseJSON(jsonList)).map(function(){$('').val(this.Id).text(this.Id).appendTo(comboboxWorkUnit);});
},

您的jsonList应该只是一个数组。它不需要在对象中。然后调用数组中的每一个。小提琴:

var comboboxWorkUnit=$(“select.workUnit”);
comboboxWorkUnit.show();
comboboxWorkUnit.empty();
var jsonList=[{
“Id”:“12345”,
“工作单元ID”:“SR0001954”,
“描述”:“来自Serena的测试服务请求”,
“WorkUnitCategory”:“ServiceRequest”},
{
“Id”:“12355”,
“工作单元ID”:“WOR001854”,
“说明”:“来自Serena的测试工单”,
“WorkUnitCategory”:“ServiceRequest”},
{
“Id”:“12365”,
“工作单元ID”:“DBR001274”,
“描述”:“来自Serena的测试数据库相关服务请求”,
“WorkUnitCategory”:“ServiceRequest”}];
$(jsonList).each(函数(){
comboboxWorkUnit.append($('').val(this.Id).text(this.Id));
});

$。parseJSON()需要一个字符串。看起来您正在向其传递一个对象。这看起来是一个有效的解决方案。但是,我建议在循环内部将生成的选项附加到字符串变量,然后将该字符串单独附加到选择框中。它将比多次操作DOM更有效。
   var comboboxWorkUnit = $("select.workUnit");

    comboboxWorkUnit.show();
    comboboxWorkUnit.empty();

    var jsonList = [{
        "Id": "12345",
        "WorkUnitId": "SR0001954",
        "Description": "Test Service Request From Serena",
        "WorkUnitCategory": "ServiceRequest"},
    {
        "Id": "12355",
        "WorkUnitId": "WOR001854",
        "Description": "Test Work Order From Serena",
        "WorkUnitCategory": "ServiceRequest"},
    {
        "Id": "12365",
        "WorkUnitId": "DBR001274",
        "Description": "Test Database Related Service Request From Serena",
        "WorkUnitCategory": "ServiceRequest"}];


    $(jsonList).each(function() {
        comboboxWorkUnit.append($('<option>').val(this.Id).text(this.Id));
    });