简体   繁体   English

在jquery中打印对象的值

[英]printing values of object in jquery

I have below code that get values from a grid. 我有以下代码从网格中获取值。 Instead of printing the values, the grid is printing data in below object format. 网格不是打印值,而是以下面的对象格式打印数据。 How to print values? 如何打印价值?

[object Object], [object Object], [object Object], [object Object], [object Object]

CODE: 码:

$(document).ready(function () {
    $("#check").click(function(){
        var rows = $('#jqxgrid').jqxGrid('getrows');
        alert(rows);
    });
});        

A simple way is to use 一种简单的方法是使用

JSON.stringify(rows)

ie

alert(JSON.stringify(rows))

Else, you would need to manually traverse the object's properties and print them accordingly. 否则,您需要手动遍历对象的属性并相应地打印它们。 Give more details about input and desired output for an example. 提供有关示例的输入和所需输出的更多详细信息。

An example in interactive Node.js: 交互式Node.js中的一个例子:

> x = { 1: 2, 3:4 };
{ '1': 2, '3': 4 }

> x.toString();
'[object Object]'

> JSON.stringify(x)
'{"1":2,"3":4}'

In order to print value of objects in JQuery you can create a new array with values and print it: 为了在JQuery中打印对象的值,您可以使用值创建一个新数组并打印它:

var array =  $.map(object, function(value){
    return value;
})

Grid's getrows returns an array of rows. Grid的getrows返回一组行。 To get the first row: var row = rows[0]. 获取第一行:var row = rows [0]。 To get the second row use: var row2 = rows[1] and so on. 要获得第二行使用:var row2 = rows [1],依此类推。

To get the 'fname' cell value from the first row, you can do this: 要从第一行获取“fname”单元格值,您可以执行以下操作:

var rows = $("#grid").jqxGrid('getrows');
var firstRow = rows[0];
var fnameValue = firstRow.fname;

Got help from stackoverflow, Include following jquery function: 从stackoverflow获得帮助,包括以下jquery函数:

   The result will be equivalent to the PHP function print_r.
   echo '<pre>' . print_r($data) . '</pre>';

   USAGE: 
   var data = [{'id':1,'name':'hello'},'world'];
   $('#element').print_r(data);

//==========================================
(function($) {

    $.fn.print_r = $.fn.print = function(variable){
        return this.each(function(){
        if(typeof variable == 'object'){
            var string = $.print_r.objectToString(variable,0);
            $(this).html(string);
        } else {
            $(this).html('<pre>'+variable.toString()+'</pre>');
        }
    });

    }

    $.print_r = {
            objectToString : function (variable,i){
              var string = '';
              if(typeof variable == 'object' && i < 3){ // 3 is to prevent endless recursion, set higher for more depth
                  string += 'Object ( <ul style="list-style:none;">';
                  var key;
                  for(key in variable) {
                      if (variable.hasOwnProperty(key)) {
                        string += '<li>['+key+'] => ';
                        string += $.print_r.objectToString(variable[key],i+1);
                        string += '</li>';
                      }
                  }
                  string += '</ul> )';
              } else {
                  string = variable.toString();
              }
              return string;
        }
    }

})(jQuery)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM