diff --git a/includes/ajax.inc b/includes/ajax.inc index 41c6983..6111d37 100644 --- a/includes/ajax.inc +++ b/includes/ajax.inc @@ -619,13 +619,24 @@ function ajax_pre_render_element($element) { case 'submit': case 'button': case 'image_button': - // Use the mousedown instead of the click event because form - // submission via pressing the enter key triggers a click event on - // submit inputs, inappropriately triggering Ajax behaviors. + // Pressing the ENTER key within a textfield triggers the click event of + // the form's first submit button. Triggering Ajax in this situation + // leads to problems, like breaking autocomplete textfields, so we bind + // to mousedown instead of click. + // @see http://drupal.org/node/216059 $element['#ajax']['event'] = 'mousedown'; - // Attach an additional event handler so that Ajax behaviors - // can be triggered still via keyboard input. + // Retain keyboard accessibility by setting 'keypress'. This causes + // ajax.js to trigger 'event' when SPACE or ENTER are pressed while the + // button has focus. $element['#ajax']['keypress'] = TRUE; + // Binding to mousedown rather than click means that it is possible to + // trigger a click by pressing the mouse, holding the mouse button down + // until the Ajax request is complete and the button is re-enabled, and + // then releasing the mouse button. Set 'prevent' so that ajax.js binds + // an additional handler to prevent such a click from triggering a + // non-Ajax form submission. This also prevents a textfield's ENTER + // press triggering this button's non-Ajax form submission behavior. + $element['#ajax']['prevent'] = 'click'; break; case 'password': diff --git a/misc/ajax.js b/misc/ajax.js index fb03e2b..830c8aa 100644 --- a/misc/ajax.js +++ b/misc/ajax.js @@ -182,10 +182,17 @@ Drupal.ajax = function (base, element, element_settings) { // can be triggered through keyboard input as well as e.g. a mousedown // action. if (element_settings.keypress) { - $(element_settings.element).keypress(function (event) { + $(ajax.element).keypress(function (event) { return ajax.keypressResponse(this, event); }); } + + // If necessary, prevent the browser default action of an additional event. + // For example, prevent the browser default action of a click, even if the + // AJAX behavior binds to mousedown. + if (element_settings.prevent) { + $(ajax.element).bind(element_settings.prevent, false); + } }; /**