2010-04-08 61 views
2

当我取消注释警报时,数据就在那里......如:这个功能怎么了? .each相关

{ 
     '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')); 

      }); 

     }); 
+0

我不认为'jquery'和'mysql'标签可以在相同的问题coesist。 – Strae 2010-04-08 16:36:03

+0

修复了标签,thx – Ritz 2010-04-08 16:38:14

+0

什么是事件数据? – 2010-04-08 17:23:03

回答

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

ID确实警告对象对象,我怎么把de zvmlist传递给我的每个函数呢? – Ritz 2010-04-08 17:43:11

+0

它可能是显而易见的中间jquery球员,但我是jquery – Ritz 2010-04-08 17:46:05

+0

新手它做了诡计,它现在工作。感谢了很多大卫! – Ritz 2010-04-08 17:51:39

2

你的服务器Content-Type: text/html代替application/json响应,所以jQuery的不EVAL你的对象。你可以指定内容类型:

$.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格式和EVAL服务器的响应。

+0

我添加了正确的标题,但仍然没有运气 <?('Cache-Control:no-cache,must-revalidate'); php header header('Expires:Mon,1997年7月26日05:00:00 GMT'); header(“Content-type:application/json; charset = utf-8”); header(“Content-Transfer-Encoding:8bit”); $ query =“SELECT id,zorgvormen_Naam FROM zorgvormen WHERE parent_zorgvormen_id = 0”; $ result = mysql_query($ query)或die(mysql_error()); echo“{”; ($ row = mysql_fetch_array($ result)){ \t echo“'”。 $行[ 'zorgvormen_Naam']。 “ ':'”。 $行[ 'zorgvormen_Naam']。 “”,”; \t } echo“}”; ?> – Ritz 2010-04-08 16:49:20

+0

您的初始测试链接不再对此脚本执行ajax调用。我添加了 – 2010-04-08 16:54:19

+0

。 JSON在我的函数结束。所以我再次删除它。 – Ritz 2010-04-08 17:00:18

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'); 
+0

我添加了这个,但仍然没有工作,thx为你的努力 – Ritz 2010-04-08 16:54:41

0

大卫·默多克p抛开“诀窍”。 这就是我的功能现在看起来如何,它的工作就像一个魅力。

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