Autocomplete Drupal在选择建议时自动完成提交

Autocomplete Drupal在选择建议时自动完成提交,autocomplete,drupal-7,Autocomplete,Drupal 7,我已经在这个网站上学习了教程:并且我的自动完成工作得很好 我不知道该怎么做,而且似乎在网上找不到的是,当用户单击建议时提交表单,而不是简单地填写该字段 提前感谢。这可能不是最好的“drupal”方式,也许有人可以改进它 在“misc/autocomplete.js”文件中找到: Drupal.jsAC.prototype.select = function (node) { this.input.value = $(node).data('autocompleteValue'); };

我已经在这个网站上学习了教程:并且我的自动完成工作得很好

我不知道该怎么做,而且似乎在网上找不到的是,当用户单击建议时提交表单,而不是简单地填写该字段

提前感谢。

这可能不是最好的“drupal”方式,也许有人可以改进它

在“misc/autocomplete.js”文件中找到:

Drupal.jsAC.prototype.select = function (node) {
  this.input.value = $(node).data('autocompleteValue');
}; 
并将其更改为:

Drupal.jsAC.prototype.select = function (node) {
  this.input.value = $(node).data('autocompleteValue');
  if(jQuery(this.input).hasClass('auto_submit')){
      this.input.form.submit();
  }
};
然后在表单项上添加:

'#attributes' => array('class'=> array('auto_submit')),
这将导致表单提交,无论用户选择如何选择(输入按钮或鼠标单击)

编辑:4年后,我得到了投票,意识到这需要更新…基本上@Azhar是正确的,而不是编辑现有的文件,这段代码应该添加到一个新的JS文件,在autocomplete.JS之后加载


任何时候编辑核心时,都必须担心核心安全更新会覆盖您的更改,并导致您的站点损坏,您需要再次修复它。

以下代码将覆盖“Drupal.jsAC.prototype.select”函数,这可以在任何JS文件中使用,而无需修改
misc/autocomplete.JS

 $(document).ready(function(){
      Drupal.jsAC.prototype.select = function (node) {
        this.input.value = $(node).data('autocompleteValue');
        if(jQuery(this.input).hasClass('auto_submit')){
          this.input.form.submit();
        }
      };
    });

或者,如果您想在所有自动完成表单上启用它,而不必使用form alter,您可以在JS文件中使用以下代码段:

Drupal.jsAC.prototype.select = function (node) {
  this.input.value = $(node).data('autocompleteValue');
  if(jQuery(this.input).hasClass('form-autocomplete')){
    this.input.form.submit();
  }
};

这仅在单击“自动完成”建议时有效,而不是通过键盘选择它

要捕获键盘和鼠标选择,请覆盖hidePopup方法

// Autosubmit on keyboard & click.
Drupal.jsAC.prototype.hidePopup = function (keycode) {
    // Select item if the right key or mousebutton was pressed.
    if (this.selected && ((keycode && keycode != 46 && keycode != 8 && keycode != 27) || !keycode)) {
      this.input.value = $(this.selected).data('autocompleteValue');
      if (jQuery(this.input).hasClass('auto-submit')) {
        this.input.form.submit();
      }
    }
    // Hide popup.
    var popup = this.popup;
    if (popup) {
      this.popup = null;
      $(popup).fadeOut('fast', function () { $(popup).remove(); });
    }
    this.selected = false;
    $(this.ariaLive).empty();
  };
截至/ ,通过在选择时触发
autocompleteSelect
事件更好地支持这一点

如果您使用的是Ajax框架,那么您可以捕获三种主要字段承诺类型(单击、输入、更新值选项卡)的更改事件,例如:


否则,您可以从自定义脚本捕获事件。它在编辑元素(更新后)上使用所选节点的一个元素数组的附加参数触发。

假设您有两个字段,例如title、description。我们假设这两个字段中都没有html标记。我们已经设置了一个视图来列出标题和描述。我们也有一个标题作为自动完成的公开过滤器

jQuery('html').on('click touchstart','.ui-autocomplete li a', function(e){
          var text = jQuery(this).html();
          jQuery('.form-autocomplete').val(text);
          jQuery('.form-autocomplete').parent().parent('form').submit();
      }); 
最详细的版本在

jQuery('html').on('click touchstart','.ui-autocomplete li a', function(e){
          var text = jQuery(this).html();
          jQuery('.form-autocomplete').val(text);
          jQuery('.form-autocomplete').parent().parent('form').submit();
      });