无法获取jQuery.parseJSON()值

时间:2013-09-02 07:28:31

标签: javascript jquery json

我在我当前的一个项目中使用jQuery,并且在合并中面临一些问题。

以下是我使用的功能如下:

$.fn.getEcval = function(c){
    var objType = c.type;  // get the value of 'pul' through alert.

    var hdf = $('input[name="eq"]').val();
    var objHdf = jQuery.parseJSON(hdf);
    alert(objHdf.objType); // get "undefined" through alert
};

$.fn.get_phnum = function(){    
    var url = $.fn.getEcval({'type':'pul'});
};

$.fn.getEcval中,我发送参数以获取值。我把JSON字符串放在我的HTML隐藏字段中。我在变量“hdf”中得到了我在函数中声明的字符串 当我在浏览器中运行它时,alert函数返回 undefined 。但是如果我把 pul (即alert(objHdf.pul))而不是 objType ,我会得到正确的输出。

以下是JSON数据:

{
  "flg": 1,
  "alrd": 1,
  "drplen": 2,
  "qrr": "7055eced15538bfb7c07f8a5b28fc5d07055eced15538bfb",
  "wfa": 0,
  "ctp": "1",
  "pul": "b7c07f8a5b28fc5"
}

在这方面的任何帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

使用数组表示法。

 alert(objHdf[objType]);