【问题标题】:Uncaught Error: cannot call methods on autocomplete prior to initialization; attempted to call method未捕获的错误:无法在初始化之前调用自动完成的方法;试图调用方法
【发布时间】:2016-09-12 08:40:10
【问题描述】:

关于Error: cannot call methods on autocomplete prior to initialization; attempted to call method 'destroy'的答案,显然我需要提出一个新问题!我正在尝试使用使用 Jquery 自动完成库的 Symfony sfWidgetFormJQueryAutocompleter 插件,JS 的 PHP 渲染函数如下。在下面的代码中,我应该在哪里添加上面帖子中提到的 $(this.target).find('input').autocomplete() 行?

sprintf(<<<EOF
<script type="text/javascript">
  jQuery(document).ready(function() {
    jQuery("#%s")
    .autocomplete('%s', jQuery.extend({}, {
      dataType: 'json',
      parse:    function(data) {
        var parsed = [];
        for (key in data) {
          parsed[parsed.length] = { data: [ data[key], key ], value:     data[key], result: data[key] };
        }
        return parsed;
      }
    }, %s))
    .result(function(event, data) { jQuery("#%s").val(data[1]); });
  });
</script>
EOF
      ,
      $this->generateId('autocomplete_'.$name),
      $this->getOption('url'),
      $this->getOption('config'),
      $this->generateId($name)
    );

【问题讨论】:

  • 我浏览了所有论坛,似乎没有人指出应该在哪里添加。用于自动完成和 JQuery / JQuery UI 加载的标准 Symfony 1.4 插件没有任何改变。

标签: javascript php jquery autocomplete symfony-1.4


【解决方案1】:

试试看:

    return $this->renderTag('input', array('type' => 'hidden', 'name' => $name, 'value' => $value)).
           parent::render('autocomplete_'.$name, $visibleValue, $attributes, $errors).
           sprintf(<<<EOF
<script type="text/javascript">
  jQuery(document).ready(function() {
    jQuery("#%s")
    .autocomplete('%s', jQuery.extend({}, {
      dataType: 'json',
      parse:    function(data) {
        var parsed = [];
        for (key in data) {
          parsed[parsed.length] = { data: [ data[key], key ], value: data[key], result: data[key] };
        }
        return parsed;
      }
    }, %s))
    .result(function(event, data) { jQuery("#%s").val(data[1]); });
  });

  // manually trigger autocomplete
  jQuery("#%s").find('input').autocomplete();
</script>
EOF
      ,
      $this->generateId('autocomplete_'.$name),
      $this->getOption('url'),
      $this->getOption('config'),
      $this->generateId($name),
      $this->generateId('autocomplete_'.$name)
    );
  }

【讨论】:

    猜你喜欢
    • 2014-02-28
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多