error al analizar jsonList a la lista desplegable Jquery

I am having issues populating a dropdown list with a JSON List. Here is what I am doing in my function.

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); }); 
    },

preguntado el 30 de enero de 12 a las 19:01

$.parseJSON() expects a string. It looks like you are passing it an object instead. -

1 Respuestas

Your jsonList should just be an array. It doesn't need to be in an object. Then call each on the array. fiddle: http://jsfiddle.net/brentmn/Jkxe2/

   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));
    });

Respondido el 31 de enero de 12 a las 00:01

This looks like a working solution. However, I would recommend appending the generated options to a string variable while inside the loop, and then performing a single append of that string to the select box. It will be more efficient than manipulating the DOM multiple times. - Y A.

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.