开发者

[object Object]undefined on input form (jquery+json+php)

开发者 https://www.devze.com 2023-01-30 08:58 出处:网络
can someone help me displaying that JSON array {\"contenusModeleCarte\"{\"idmodele\":\"65\",\"modeleName\":\"\",\"typerepas\":null,\"commentairesModeleCarte\":\"premier

can someone help me displaying that JSON array

{"contenusModeleCarte"{"idmodele":"65","modeleName":"","typerepas":null,"commentairesModeleCarte":"premier commentaire pour le premier modele de carte","dessertCarte0":"pommes au four "},{"idmodele":"65","modeleName":"","typerepas":null,"commentairesModeleCarte":"premier commentaire pour le premier modele de carte","dessertCarte1":"salade de fruits"},{"idmodele":"65","modeleName":"","typerepas":null,"commentairesModeleCarte":"premier commentaire pour le premier modele de carte","dessertCarte2":"bananes"}]}

into form inputs.

When I try to do it via this code

$.each(dataModeleCarte.contenusModeleCarte, function(i,contenusModeleCarte){
  if(co开发者_开发知识库ntenusModeleCarte.dessertCarte+i == null){
     $("#edit-field-dessert-carte-"+ i +"-value-field-dessert-value").val('');
  }else{
     $("#edit-field-dessert-carte-"+ i +"-value-field-dessert-value").val(contenusModeleCarte.dessertCarte);
  }
});

I only get as a result :

[object Object]undefined

any help would be greatly appreciated.

Thx


Your "dessertCarte" in the JSON has the numbers 0, 1, 2, ... after it, so it can't find a contenusModeleCarte.dessertCarte

You need to append the index i after "dessertCarte":

 .val(contenusModeleCarte["dessertCarte" + i]);


Is that the entire JSON string? If it is, one issue is you are missing the first colon and square bracket. It should read:

{"contenusModeleCarte":[{"idmodele":"65","modeleName":"","typerepas":null,"commentairesModeleCarte":"premier commentaire pour le premier modele de carte","dessertCarte0":"pommes au four "},{"idmodele":"65","modeleName":"","typerepas":null,"commentairesModeleCarte":"premier commentaire pour le premier modele de carte","dessertCarte1":"salade de fruits"},{"idmodele":"65","modeleName":"","typerepas":null,"commentairesModeleCarte":"premier commentaire pour le premier modele de carte","dessertCarte2":"bananes"}]}

0

精彩评论

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