1

items 是我在下面的函数中使用的变量... items 值是

[{"daLevel":"DA0","daName":"Da Name 0"},{"daLevel":"DA1","daName":"Da Name 1"},{"daLevel":"DA2","daName":"Da Name 2"},{"daLevel":"DA3","daName":"Da Name 3"},{"daLevel":"DA4","daName":"Da Name 4"},{"daLevel":"DA5","daName":"Da Name 5"},{"daLevel":"DA6","daName":"Da Name 6"},{"daLevel":"DA7","daName":"Da Name 7"},{"daLevel":"DA8","daName":"Da Name 8"},{"daLevel":"DA9","daName":"Da Name 9"},{"daLevel":"DA10","daName":"Da Name 10"}]

我需要在选择框中显示 daName 值作为下拉列表。我无法从 items var 中获取 daName 值。任何建议都会对 gr8 有所帮助。提前致谢

function notifyDa(excessId) {
alert("notified");
var html = "<table><tr><td align='center' colspan='2'> Excess Notification </td></tr><tr><td>Select DA Holder</td><td><select id='daList'>";
    var ctx = '${contextPath}';
    var queryUrl = ctx + "/excessList.htm?getDaList=true";
    $.ajax({
        url : queryUrl,
        type : "POST",
        dataType : "text",
        success : function(result) {
            alert(result);
            **var items = result;
            alert("items *** "+items);
            alert("items[0] *** "+items.daName[0]);**


            $('#notifyDiv').empty();
            $('#notifyDiv').html(html);

            $("#pop").click(function() {
                $("#notifyDiv").fadeIn(1000);
                if (!$("#notifyDiv").is(':visible')) {
                    return;
                }
            });

            $("#notifyDiv").css({
                left : ($(window).width() - $('#notifyDiv').width()) / 2,
                top : ($(window).width() - $('#notifyDiv').width()) / 7,
                position : 'absolute'
            });

        },
        error : function() {

        }
    });

}

4

2 回答 2

1

你可以试试这个

result = jQuery.parseJSON(result);  // as dataType is Text



items[0].daName;
于 2013-04-10T09:12:59.490 回答
0

您需要将Datatype更改为JSON,然后您可以使用 jQuery .each() 函数来解析您得到的 JSON。像这样的东西:

$(items).each(function(index,element){
    alert(element.daName); 
});

这将遍历所有对象并为您提供所需的字段。

于 2013-04-10T09:15:48.543 回答