kombinuję i nie mam pojęcia co zrobić. Wewnątrz formularza używam autocomplete z jQuery UI i chciałbym by w momencie wyboru elementu z listy i po naciśnięciu klawisza [ENTER] formularz nie był wysyłany. Po prostu chciałbym by wybrana wartość wskoczyła w pole i tyle.
Kombinowałem z przechwyceniem klawisza [ENTER] na polu (wychwytuje), ale nie wiem co zrobić dalej. e.preventDefault() nie zadziałało, return false też nie.
Oto mój kod:
public function display() { $return = form::input($this->get_name(), $this->get_value(), form::attributes($this->get_attributes())); $return .= ' <script type="text/javascript"> $(document).ready(function() { $("#'.$this->get_attribute('id').'").autocomplete( '.$this->render_options().' ); $("#'.$this->get_attribute('id').'").keydown(function(e) { var key = e.charCode || e.keyCode || 0; if (key == 13) { alert("Sraka"); //?
} }); }); </script> '; return $return; }
PS: komunikat "Sraka" się pojawia w momencie wciśnięcia [ENTER] na liście podpowiedzi.
// Edit:
Widzę, że jednak jest to bug autocomplete :/
Cytat
The list of suggestions
[...]
a Enter keypress will select the selected item (preventing the default form submit) or do nothing special when there is no selected item (allow the default form submit)
[...]
a Enter keypress will select the selected item (preventing the default form submit) or do nothing special when there is no selected item (allow the default form submit)
http://wiki.jqueryui.com/Autocomplete
Wie ktoś co z tym fantem zrobić?
// Edit: (w miarę rozwiązane)
Rozwiązałem to w ten sposób, może komuś się przyda lub ktoś poda lepsze rozwiązanie:
public function display() { $return = form::input($this->get_name(), $this->get_value(), form::attributes($this->get_attributes())); $return .= ' <script type="text/javascript"> $(document).ready(function() { $("#'.$this->get_attribute('id').'").autocomplete( '.$this->render_options().' ); $("#'.$this->get_attribute('id').'").keydown(function(e) { var key = e.charCode || e.keyCode || 0; if (key == 13) { var stop = function(e) { e.preventDefault(); }; $("#'.$this->get_form()->get_attribute('id').'").bind("submit", stop); setTimeout(function() { $("#'.$this->get_form()->get_attribute('id').'").unbind("submit", stop); }, 0.1 * 1000); } }); }); </script> '; return $return; }