未捕获错误:无法在初始化之前调用自动完成方法;试图调用方法

时间:2016-09-12 08:40:11

标签: javascript php jquery autocomplete symfony-1.4

关于Error: cannot call methods on autocomplete prior to initialization; attempted to call method 'destroy'的答案,显然我需要问一个新问题!我正在尝试使用Symfony sfWidgetFormJQueryAutocompleter插件,该插件使用Jquery自动完成库,JS的PHP呈现函数如下。我在哪里可以添加以下代码中上面帖子中提到的$(this.target).find('输入')。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)
    );

1 个答案:

答案 0 :(得分:0)

试试:

    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)
    );
  }