Drupal autocomplete submit on selection of suggestion
09.08.2015
Как-то раз потребовалось сделать сабмит на выборе элемента из autocomplete. Нашел такой вот способ:
// Надо заменить в файле 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();
}
};
И добавить класс к input
'#attributes' => array('class'=> array('auto_submit')),
Если вы не хотите менять ядро
$(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();
}
};
});
Если надо отслеживать нажатие клавиш на клавиатуре
// 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();
};
#Drupal
