简体   繁体   English

javascript:object对象而不是set =(

[英]javascript: object Object instead of value from set =(

So, I'm trying to have same data plotted based on a drop down filter, but I haven't gotten past the storing the data to the variable passing to the plotting function. 因此,我试图基于下拉过滤器来绘制相同的数据,但是我并没有过去将数据存储到传递给绘图函数的变量中。 my alert gives object Object , but shouldn't it be totalValue (for this example)? 我的警报给出了object Object ,但是它不应该是totalValue (在此示例中)吗?

the html HTML

   <select id="performance_chart_filter">
  <option value="totalValue">Total by Value</option>
  <option value="openValue">Open by Value</option>
  <option value="declinedValue">Declined by Value</option>
  <option value="acceptedValue">Accepted by Value</option>
</select>

the javascript JavaScript

$j("#performance_chart_filter").change(function(){
    plotWithChoice();
});



function plotWithChoice() {
   var d = {
        "totalValue": {
            label: "Total",
            clickable: true,
            hoverable: true,
            data: <%= @total %>
        }
.
.
.
};

    var filter = $j("#performance_chart_filter");
    var data = [d[filter.val()]];
alert(data);

No. filter.val() would most likely be the string "totalValue" . filter.val()最有可能是字符串"totalValue"

d[filter.val()] (or d["totalValue"] ) would be the value of the property totalValue of the object d , which is an object thus the output Object object of the alert , because that's the default string representation of a generic object like this. d[filter.val()] (或d["totalValue"] )将是对象d的属性totalValue的值,该对象dalert的对象,因此是alert的输出Object object ,因为这是的默认字符串表示形式这样的通用对象。

And [d[filter.val()]] is a Array with one element namely the object mentioned above. 并且[d[filter.val()]]是一个数组,其中一个元素即上述对象。

To debug this, I'd suggest you use a Debugger (such as Firebug), because that can display objects in a readable form. 要进行调试,建议您使用调试器(例如Firebug),因为它可以以可读形式显示对象。

Or use the .toSource() method: 或使用.toSource()方法:

alert(data.toSource());

If all the members of the d object are similar to the one shown, and d is in scope, then alert(data) will always will show "object Object". 如果d对象的所有成员都与所示成员相似,并且d在范围内,则alert(data)将始终显示“ object Object”。

If filter.val() is "totalValue", then data will be the Object: 如果filter.val()为“ totalValue”,则数据将为对象:

{
  label: "Total",
  clickable: true,
  hoverable: true,
  data: <%= @total %>
}

This doesn't have an explicit toString method, so the default toString will be used. 它没有显式的toString方法,因此将使用默认的toString

Try alert(data.label) , hopefully this will show "Total". 尝试alert(data.label) ,希望它会显示“ Total”。

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

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