dopgv00024 2016-09-12 08:40
浏览 231

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

Regarding the answer to Error: cannot call methods on autocomplete prior to initialization; attempted to call method 'destroy', apparently I need to ask as a new question! I am trying to use the Symfony sfWidgetFormJQueryAutocompleter plugin which uses the Jquery autocomplete library, the PHP render function for the JS is as follows. Where would I add the $(this.target).find('input').autocomplete() line mentioned in the above post in the below code?

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条回答 默认 最新

  • dougehe2022 2016-09-22 15:34
    关注

    Try that:

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

报告相同问题?