这个功能怎么了? .each相关

时间:2010-04-08 16:33:27

标签: jquery ajax

当我取消注释警报时,数据就在那里......如:

{ 
      'Huishoudelijke hulp': 'Huishoudelijke hulp', 
      'Verpleging thuis': 'Verpleging thuis',
      'Verzorging thuis': 'Verzorging thuis',
      '24 uurs zorg': '24 uurs zorg',
      'Ondersteunende begeleiding': 'Ondersteunende begeleiding',
    }   

但是不是填充键和值,而是整个var,并开始为每个字符创建一个键和值对。

你可以在这里看到这个: http://www.zorgzuster-zeeland.nl/site/static/calendar_test.php

在日历中创建任务,然后尝试通过单击来编辑任务。 它应该正确填充下拉字段。

当我创建具有相同值的静态var时,下拉列表可以正常工作。

静态变量

var zvmlist = { 
      'Huishoudelijke hulp': 'Huishoudelijke hulp', 
      'Verpleging thuis': 'Verpleging thuis',
      'Verzorging thuis': 'Verzorging thuis',
      '24 uurs zorg': '24 uurs zorg',
      'Ondersteunende begeleiding': 'Ondersteunende begeleiding',
    }; 

这是我的功能,任何人都有线索?

$.get('get_zorgvormen.php', function(zvmlist) {
        //alert("Data Loaded: " + zvmlist);

            $.each(zvmlist, function(key, value) { 
              var selected='';
              if(key==eventdata.title){var selected='selected' }
              $('<option value="'+key+'" '+selected+'>'+value+'</option>').appendTo($('#calendar_edit_entry_form_title')); 

            }); 

        }); 

4 个答案:

答案 0 :(得分:2)

您的服务器使用Content-Type: text/html而不是application/json进行响应,因此jQuery不会评估您的对象。您可以指定内容类型:

$.get('get_zorgvormen.php', function(zvmlist) {
    $.each(zvmlist, function(key, value) { 
        var selected = '';
        if(key==eventdata.title) { 
            selected = 'selected';
        }
        $('<option value="'+key+'" '+selected+'>'+value+'</option>')
            .appendTo($('#calendar_edit_entry_form_title')); 
   }); 
}, 'json'); 

但我建议您修复server side script以发送正确的内容类型,jquery会自动识别json格式并评估服务器的响应。

答案 1 :(得分:0)

你不要告诉Jquery期待json格式。

来自文档:

jQuery.get( url, [ data ], [ callback(data, textStatus, XMLHttpRequest) ], [ dataType ] )

urlA string containing the URL to which the request is sent.

dataA map or string that is sent to the server with the request.

callback(data, textStatus, XMLHttpRequest)A callback function that is executed if the request succeeds.

dataTypeThe type of data expected from the server.

source

尝试指定您期望Json:

$.get('get_zorgvormen.php', function(zvmlist) {
        //alert("Data Loaded: " + zvmlist);

            $.each(zvmlist, function(key, value) { 
              var selected='';
              if(key==eventdata.title){var selected='selected' }
              $('<option value="'+key+'" '+selected+'>'+value+'</option>').appendTo($('#calendar_edit_entry_form_title')); 

            }); 

        }, 'json');

答案 2 :(得分:0)

试试这个:

$.ajax({
    "url":"get_zorgvormen.php",
    "dataType":"json",
    "type":"get",
    "success":function(zvmlist){
        // should alert [Object object] or something similar
        alert(zvmlist);
    },
    "error":function(msg){
        alert(msg);
    }
});

答案 3 :(得分:0)

大卫·默多克提供了“诀窍”。 这就是我的功能现在的样子,它的工作就像一个魅力。

$.ajax({
        "url":"get_zorgvormen.php",
        "dataType":"json",
        "type":"get",
        "success":function(zvmlist){

            $.each(zvmlist, function(key, value) { 
              var selected='';
              if(key==eventdata.title){selected='selected' }
              $('<option value="'+key+'" '+selected+'>'+value+'</option>').appendTo($('#calendar_edit_entry_form_title')); 

            });
        },
        "error":function(msg){
            alert(msg);
        }
    });