0

我有一个来自服务器的 JSON 响应,响应是一个包含三列的列表对象。但是,要填写选择选项列表,我只需要使用两个。我能够在客户端上接收数据,但是我无法填写选择选项列表。

jQuery

$("select#offenceCatId").change(function(){

    $.ajax({
        type:'GET',
        url:'getCrimeTypeList.htm',
        data:{crimeCatId: $(this).val()},

        headers: {
            Accept: 'application/json'
        },
        dataType: 'json',
        success:function(data){
            var crimeType = JSON.stringify(data);
            var options = '';

            for (var i = 0; i < crimeType.length; i++) {
                options += '<option value="' + crimeType.crimeTypeId[i] + '">' + crimeType.crimeTypeDesc[i] + '</option>';
            }
            $("select#offenceTypeId").html(options);
        }
    });
});

JSON数据

[{"crimeTypeDesc":"煽动谋杀","crimeCatId":4,"crimeTypeId":39},{"crimeTypeDesc":"性和非性侵犯","crimeCatId":4,"crimeTypeId":40} ,{"crimeTypeDesc":"Pornography","crimeCatId":4,"crimeTypeId":41},{"crimeTypeDesc":"Censorship","crimeCatId":4,"crimeTypeId":42},{"crimeTypeDesc": "法定强奸","crimeCatId":4,"crimeTypeId":43},{"crimeTypeDesc":"艾滋病毒的犯罪传播","crimeCatId":4,"crimeTypeId":44},{"crimeTypeDesc":"通奸","crimeCatId":4,"crimeTypeId":45}]

我只需要用crimeTypeDesc 和crimeTypeId 填写选择选项列表

错误类型错误 :crimeType.crimeTypeId 未定义

4

3 回答 3

2

如果您要取回 JSON 数据,则不需要该stringify调用。您应该能够用以下内容替换您的success方法:

success:function(crimes){       
    var options = '';

    for (var i = 0; i < crimes.length; i++) {
        var crime = crimes[i];
        options += '<option value="' + crime.crimeTypeId + '">' + crime.crimeTypeDesc + '</option>';
    }
    $("select#offenceTypeId").html(options);
}
于 2013-03-29T16:38:09.233 回答
1

You do not need to stringify the json object as that will flatten the object and you will no longer be able to use dot notation to access it's properties. Also, your other problem is the way you are trying to access these values. You say, crimeType.crimeTypeId[i] but you need to use crimeType[i].crimeTypeId as the index refers to which object within crimeType you are referring not which id within the crypeTypeIds you want (there is only one anyway) Here is a fiddle with the working script minus the ajax call. all you need to do is replace what you have in your success callback with what I have in the fiddle minus the json object. You can adjust it after that to your needs. I left a console log because I didn't have your html.

fiddle http://jsfiddle.net/XJsH2/

$(document).ready(function(){
    var crimeType = [{"crimeTypeDesc":"Incitement To Murder","crimeCatId":4,"crimeTypeId":39},{"crimeTypeDesc":"Sexual and Non Sexual Assults","crimeCatId":4,"crimeTypeId":40},{"crimeTypeDesc":"Pornography","crimeCatId":4,"crimeTypeId":41},{"crimeTypeDesc":"Censorship","crimeCatId":4,"crimeTypeId":42},{"crimeTypeDesc":"Statutory Rape","crimeCatId":4,"crimeTypeId":43},{"crimeTypeDesc":"Criminal Transmission Of HIV","crimeCatId":4,"crimeTypeId":44},{"crimeTypeDesc":"Adultery","crimeCatId":4,"crimeTypeId":45}];
    var options = '';
    for (var i = 0; i < crimeType.length; i++) {
    options += '<option value="' + crimeType[i].crimeTypeId + '">' + crimeType[i].crimeTypeDesc + '</option>';
    }
    console.log(options);
});
于 2013-03-29T16:45:08.473 回答
1

You dont need to stringify the data, because the function returns a JSON object. jQuery has a for each funktion, which you can use to go through arrays.

success: function(data)
{
  var options = "";
  $.each(data, function(crime)
  {
    options += '<option value="' + crime.crimeTypeId + '">' + crime.crimeTypeDesc + '</option>';
  });
  $("select#offenceTypeId").html(options);
}
于 2013-03-29T16:47:04.407 回答