JS/JQuery:
$this.find('input').autocomplete({
source: "/autocomplete_tc_testcasename",
minLength: 2,
focus: function(e,ui){
$(this).val(ui.item.label);
return false;
},
select: function(e, ui) {
console.log("Selected: "+ui.item.value);
}
});
CSS:
.ui-autocomplete {
max-height: 200px;
overflow-y: auto;
padding: 5px;
}
.ui-menu {
list-style: none;
background-color: #FFFFEE;
width: 50%;
padding: 0px;
border-bottom: 1px solid #DDDDDD;
border-radius: 6px;
-webkit-box-shadow: 0 8px 6px -6px black;
-moz-box-shadow: 0 0px 0px -0px black;
box-shadow: 0px 2px 2px 0px #999999;
}
.ui-menu .ui-menu {
}
.ui-menu .ui-menu-item {
color: #990000;
font-family:"Verdana";
font-size: 12px;
border-top: 3px solid #DDDDDD;
background-color: #FFFFFF;
padding: 10px;
}
Problem Summary:
I found various other questions quite in the ballpark, but none of these seem to address my question.
jQuery Autocomplete - Left Mouse Click not firing Select event
jQuery UI autocomplete select event not working with mouse click
Thanks!
I was facing a similar problem. I wanted to submit the form when the user clicked on an option. But the form got submitted even before the value of the input could be set. Hence on the server side the controller got a null value.
I solved it using a modified version of William Niu's answer on another related post - jQuery UI autocomplete select event not working with mouse click
I basically checked for the event type. If it was a click then I explicitly set the input box's value to the value in ui.item.value. See the snippet below,
jQuery( "#autoDropDown" ).autocomplete({
source: [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ], minLength: 0, delay:0,
select: function( event, ui ) {
var origEvent = event;
while (origEvent.originalEvent !== undefined){
origEvent = origEvent.originalEvent;
}
//console.info('Event type = ' + origEvent.type);
//console.info('ui.item.value = ' + ui.item.value);
if (origEvent.type == 'click'){
document.getElementById('autoDropDown').value = ui.item.value;
document.getElementById('myForm').submit();
} else {
document.getElementById('myForm').submit();
}
}
});
I have the same issue, but in my case it is even more strange. the select event is fired every second time (when using the mouse). for me the select event is also fired just fine in case i use my keyboard.
unfortunately I could not reproduce this issue via jsfiddle. but i still want to offer my jsfiddle, maybe it is a good starting point for further analysis. here is my jsFiddle code: http://jsfiddle.net/knzNg/
The only difference is that I use a local array as datasource for the autocomplete data. I guess it is better to change the code to use a remote datasource.
I tested it in all version of IE (inlcuding 9) and always ended up with an empty input-control after I selected the an item using the mouse. This caused some headaches. I even went down to the source code of jQuery UI to see what happens there but didn’t find any hints either. We can do this by setting a timeout, which internally queues an event in the javascript-engine of IE. Because it is guaranteed, that this timeout-event will be queued after the focus event (this has already been triggered before by IE itself)
select: function (event, ui) {
var label = ui.item.label;
var value = ui.item.value;
$this = $(this);
setTimeout(function () {
$('#txtBoxRole').val(value);
}, 1);
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With