【问题标题】:Is there any javascript function, or even on the jQuery Library that does the same as PHP print_r()?是否有任何 javascript 函数,甚至在 jQuery 库上与 PHP print_r() 一样?
【发布时间】:2011-03-14 05:59:29
【问题描述】:

大家好
是否有任何 javascript 函数,甚至在 jQuery 库 中(我想是的,因为 jQuery 有 JSON 库并且能够序列化)和 PHP print_r() 函数一样吗?

我已经用谷歌搜索过,但我发现只有打印一维二维数组的函数。

提前致谢...
何塞·莫雷拉

编辑:
问:我为什么要问这个?
答:实际上我有一个$.ajax() 调用,它接收到这样的 JSON 字符串(为保护隐私,对数字进行了编辑):

{"sms":{"92255221":{"status":true,"debug":"ok","warnmsg":"SMS Sended!"},"92255222":{"status":true,"debug":"ok","warnmsg":"SMS Sended!"},"92255223":{"status":true,"debug":"ok","warnmsg":"SMS Sended!"},"92255224":{"status":true,"debug":"ok","warnmsg":"SMS Sended!!"},"92255225":{"status":true,"debug":"ok","warnmsg":"SMS Sended!"},"92255226":{"status":true,"debug":"ok","warnmsg":"SMS Sended!"},"92255227":{"status":true,"debug":"ok","warnmsg":"SMS Sended!"},"92255228":{"status":true,"debug":"ok","warnmsg":"SMS Sended!"}}}

在我的success: function() 上,我有这样的想法:

success: function(response){
                    var data = $.parseJSON(response);
                    img_ok = "<img src=\"http://www.mysite.com/images/icons/icon-ok-16.png\" />";
                    img_fail = "<img src=\"http://www.mysite.com/images/icons/icon-fail-16.png\" />";
                    for (i=0;i<=mobilenumbers.length;i++){
                        var selector = "input.client[value*="+mobilenumbers[i]+"]";
                        // Remove input checkbox
                        $(selector).remove();
                        // Replace by an image
                        if(data['sms'][mobilenumbers[i]]['status']){
                            $(selector).filter(function(){return $(this).attr("checked");}).parent().append(img_ok);
                        }else{
                            $(selector).filter(function(){return $(this).attr("checked");}).parent().append(img_fail);
                        }*/
                    } 

但萤火虫说data['sms'][mobilenumbers[i]] 未定义...但奇怪的是第一个data['sms'][mobilenumbers[i]]['status'] 工作正常!

【问题讨论】:

  • 你的错误在这里 i
  • @epascarello 我试过了
  • @ALL 问题已解决,php 返回错误数字,array_merge_recursive() 函数中出现错误...感谢所有

标签: javascript jquery function multidimensional-array printing


【解决方案1】:

好问题!不知道有没有,有兴趣看看有没有。

同时,一些替代方案:

  • 在打开 Firefox 的 Firebug 的同时执行 console.log(your_object) 将为您提供一个不错的、可浏览的树形视图。

  • 在 IE 8 的开发人员工具中也是如此,但它有点棘手。见this question

【讨论】:

  • 但我需要将console.log(object) 放在我的脚本上或放到萤火虫的某个地方?
  • +1 ;) 请检查我的编辑以了解我的问题... PS:很高兴再次见到你 ;) 哈哈
【解决方案2】:

如果要在字符串中打印 javascript 对象,则需要对其进行序列化。 jQuery 目前只有解析 JSON 函数

This 或本机 JSON.strigify 函数将为您提供字符串。然后你可以使用 javascript prettifier 来缩进它,如果你愿意的话 (here)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 2011-06-26
    • 2017-12-25
    • 1970-01-01
    • 2013-12-11
    相关资源
    最近更新 更多