dqsong2010
2016-08-25 06:07
浏览 50

使用存储在用户meta - Wordpress中的JS数组

I am storing an JS array in user meta. Then retrieving that array I am using below code

<input type="hidden" name= "my_array" id= "my_array" 
   value="<?php echo get_user_meta( get_current_user_id(), "ARRAY_NAME",false);?>" />

In console it shows me like this

<input id="my_array" type="hidden" value="Array" name="my_array">

Now I want to use this array in my JS code so I did this

var my_data = $("#my_array").val();
     console.log("confffi");
    console.log(my_data );
    console.log("confffi");

Output is

confffi

"Array"

confffi

How to get my JS array and use it in JS ?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanpo7282 2016-08-25 06:20
    已采纳

    I'm assuming your function get_user_meta returns an array (at least from the output of your code) If that is the case, try this:

    var my_data = JSON.parse('<?php echo json_encode(get_user_meta( get_current_user_id(), "ARRAY_NAME",false));?>');
     console.log("confffi");
    console.log(my_data );
    console.log("confffi");
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题