I have a select2 search function and when a user presses enter within the search box the the field will be populated with the first result pulled from the search request.
I would like upon keypress enter to submit the form.
js for the select2 looks like this:
$(document).ready(function () {
$(".js-search-menus").select2({
multiple: true,
maximumSelectionSize: 1,
placeholder: 'Find a Menu',
allowClear: true,
ajax: {
type: 'POST',
url: '{{ action('\Modules\Admin\Http\Controllers\Restaurant\Menus\MenusController@postGetMenus') }}',
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term,
page: params.page
};
},
processResults: function (data, params) {
menus = []
$.each($.parseJSON(data), function () {
menus.push({
id: this.MenuID,
text: this.MenuTitle
});
});
return {
results: menus,
pagination: {
more: (params.page * 30) < menus.length
}
};
},
cache: true
},
minimumInputLength: 3
});
});
works fine.
tried to add this after to submit on enter, didn't work?
$(document).ready(function () {
$('select').keypress(function (e) {
if (e.which == 13) {
$('#searchForm').submit();
return false;
}
});
});