I'm using the jQueryUI autoplete()
function and I can't figure out how to have my form submit when an item is selected.
I think the issue is with the select: event
but I'm new with jQueryUI and can't figure out how to make this work.
Here's my code which works fine otherwise:
<script type="text/javascript">
$(document).ready(function() {
$(function() {
$( "#search_box" ).autoplete({
source: function(request, response) {
$.ajax({ url: "<?php echo site_url('autoplete/suggestions'); ?>",
data: { term: $("#search_box").val()},
dataType: "json",
type: "POST",
success: function(data){
response(data);
},
select: function (event, ui) {
$(event.target).val(ui.item);
$('#search_form').submit();
return false;
}
});
},
minLength: 1
});
});
});
</script>
Any assistance would be greatly appreciated!
I'm using the jQueryUI autoplete()
function and I can't figure out how to have my form submit when an item is selected.
I think the issue is with the select: event
but I'm new with jQueryUI and can't figure out how to make this work.
Here's my code which works fine otherwise:
<script type="text/javascript">
$(document).ready(function() {
$(function() {
$( "#search_box" ).autoplete({
source: function(request, response) {
$.ajax({ url: "<?php echo site_url('autoplete/suggestions'); ?>",
data: { term: $("#search_box").val()},
dataType: "json",
type: "POST",
success: function(data){
response(data);
},
select: function (event, ui) {
$(event.target).val(ui.item);
$('#search_form').submit();
return false;
}
});
},
minLength: 1
});
});
});
</script>
Any assistance would be greatly appreciated!
Share Improve this question edited Jul 12, 2012 at 17:31 user229044♦ 240k41 gold badges344 silver badges347 bronze badges asked Jul 8, 2011 at 2:49 tim petersontim peterson 24.4k63 gold badges186 silver badges303 bronze badges 7- are there any console errors? look in firebug. – Dave L. Commented Jul 8, 2011 at 2:53
- also you have never accepted an answer to your questions, so people might be less eager to help you. – Dave L. Commented Jul 8, 2011 at 2:54
-
1
@tim: Your code works if copy-pasted (minus the AJAX): jsfiddle/uXHCQ with one tweak (
ui.item.value
instead ofui.item
). What do you mean when you say your form doesn't submit? – Andrew Whitaker Commented Jul 8, 2011 at 2:58 - nothing related to this query, any other ideas? – tim peterson Commented Jul 8, 2011 at 3:02
- hi Andrew, I mean that i want the form to submit once one of the items is selected, just the standard thing like Google does... – tim peterson Commented Jul 8, 2011 at 3:15
3 Answers
Reset to default 4Andrew was correct, see the fiddle he mentioned. If you switch the part with "ui.item" to "ui.item.value" the select: function() now works perfectly.
Yes it works
Except if you expect to get the value in your server-side script in the page called by action...
For now, no way to find why auto-submit of the form after selection by mouse or by keyboard, and the $(event.target).val(ui.item.value)
line, don't deliver the value in $_POST
array
Yeah Alice is also right. You just need to add this line in your onclick function:
$('#search_form').submit();
where search_form is the id of your form.