开发者

Got a static var which is working, now need to get dynamic var which isn't working

I've got this variable:

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

this var is used in a function to create a drop-down list:

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

which works like a charm.

Now I created a function to fetch the list from a MySQL table:

 $.get('get_zorgvormen.php', function(data) {

    var zvmlist = '{'+data+'}';
    //alert(zvmlist);

  });

When I enable the alert function, it shows me the var(array).

But I it won't act as a variable.

How can I pass this data to my .each function?

(You can see it in action here: http://www.zorgzuster-zeeland.nl/site/static/calendar_test.php.)


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

when the var zvmlist= currently

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

but i want to get it populated by:

$.get('get_zorgvormen.php', function(data) {
      //data = {"key":"value"};
      var obj = {};
      obj[0] = data;
      var zvmlist = '{'+obj[0]+'}';
      alert(zvmlist);
    });

When i delete the static var the app breaks and wont populate the dropdown


I combined both functions, it is kind of working except, the dropdownlist is populated w开发者_JAVA百科ith each letter from the var.

like

<option value="1">1</option>
<option value="2">'</option>
<option value="3"> </option>
<option value="4">:</option>
<option value="5"> </option>
<option value="6">'</option>
<option value="7">H</option>
<option value="8">u</option>

and so on

$.get('get_zorgvormen.php', function(data) {
      //data = {"key":"value"};
      var obj = {};
      obj[0] = data;
      var zvmlist = obj[0];  
     $.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')); 
        });
     });

any help will be appriciated


i think you have to evaluate that string so it becomes useful. Try:

var zvmlist = eval('{'+data+'}');


You can parse JSON data using the eval() function.

$.get('get_zorgvormen.php', function(data) { 
   return eval('({' + data + '})');
}); 


Without using eval, you could create an object literal first, then pass that to zvmlist.

  $.get('get_zorgvormen.php', function(data) {
      //data = {"key":"value"};
      var obj = {};
      obj[0] = data;
      var zvmlist = obj[0];
      alert(zvmlist);
  });

not tested, but something like this.


Instead of

var zvmlist = '{'+data+'}';

Try

var zvmlist = Eval('{' + data + '}');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜