【问题标题】:Uncaught TypeError: Object (JS Function) has no method 'apply'未捕获的类型错误:对象(JS 函数)没有“应用”方法
【发布时间】:2014-01-29 19:53:01
【问题描述】:

我想用 django-chartit 图表处理事件点击,下面是 python 代码。

def basicpie(request, title, code, doc, sidebar_items):

    def monthname(month_num):
        names ={1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun',
            7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
        return names[month_num]


    ds = DataPool(
       series=
        [{'options': {
            'source': MonthlyWeatherByCity.objects.all()},
          'terms': [
            'month',
            'boston_temp']}
         ])

    cht = Chart(
        datasource = ds,
        series_options =
          [{'options':{
              'type': 'pie',
              'stacking': False},
            'terms':{
              'month': [
                'boston_temp']
              }}],
        chart_options =
          {'title': {
               'text': 'Monthly Temperature of Boston'},

            'plotOptions': {
              'series': {
                "cursor": "pointer",
                'point': {
                  'events': {
                    'click': 'function() {alert(\'clicked\');}'
                  }
                }
              }
            }
          },
        x_sortf_mapf_mts = (None, monthname, False))

    return render_to_response('chart_code.html', {'chart_list': cht,
                                         'code': code,
                                         'title': title,
                                         'doc': doc,
                                         'sidebar_items': sidebar_items})

和django-chartit的javascript代码

$(document).ready(function() {
    $.each(_chartit_hco_array, function(index, chartoptions) {
        chart = new Highcharts.Chart(chartoptions);
    });
});

但是当我点击图表时出现以下错误。

Uncaught TypeError: Object function() {alert('clicked');} has no method 'apply'.

请帮我解决这个问题。

【问题讨论】:

  • 应该是'click': function() {alert('clicked');}
  • 为什么'click': 'function() {alert(\'clicked\');}' 在引号中?
  • "foobar".apply() 抛出同样的错误
  • 我的猜测:因为它可能来自错误的 JSON 复制/粘贴。出于某种原因,语法高亮对于新手来说是不可见的,当它应该是出现问题的第一条线索时。
  • @elclanrs 在我看来,他似乎打算用引号括起来,因为他避开了内部引号。

标签: javascript jquery python django highcharts


【解决方案1】:

Highcharts 需要一个用于点击参数的函数,而您正在传递一个字符串。

改用这个

'events': {
         'click': function() {
              alert("clicked");
             }
          }

Here 是 Highcharts 中的文档。

【讨论】:

  • 感谢@Kelly J Andrews。我想从 python 代码生成代码 js。如果我直接用javascript代码运行,是正常的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
相关资源
最近更新 更多