I have an input field that is connected to the jQuery autocomplete function. I was trying to get a select field to also trigger the autocomplete to refine the search results.
I have things working the way I would like them to but it isn't clear to me WHY the select input field is triggering the AutoComplete
function. The county field would normally be hidden until some search results were found but I've left it exposed to simplify things a little.
I understand why the ModSearch
input field triggers the AutoComplete
function but not why the county
field does too (it is only referenced in the AutoComplete
function itself). It is worth noting that the county
field does NOT trigger the AutoComplete
until at least 1 character has been entered into the ModSearch
field.
The code below is self contained and if it saved as PHP and called from a browser it will simply echo the contents of $_GET
after something is entered into the search field.
<?php
if (!empty($_GET['look4'])) {
echo '<pre>';
print_r($_GET);
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script>
function AutoComplete() {
$("#ModSearch,#county").autocomplete({
source: function(request, response) {
var look4 = $("#ModSearch").val();
var county = $("#county").val();
var request = $.ajax({
url: 'SO_Question.php',
data: { look4 : look4 ,
county : county} ,
dataType: "text",
cache: false
})
request.done(function( response ) {
document.getElementById('results').innerHTML = response;
document.getElementById("results").style.display = "block"
});
}, // end of source
}); // end of autocomplete
} // end of the AutoComplete function
$(function() {
$('#ModSearch').on('input',function() {
AutoComplete();
});
}); // end of function
</script>
</head>
<body>
<p>
Start typing to search: <input type="search" id="ModSearch" placeholder="Start typing to search">
<section>
You may refine your search by choosing a county
<select name="county" id="county" size="1">
<option value="">-- Select County --</option>
<option value="91">ABC</option>
<option value="92">DEF</option>
<option value="93">XYZ</option>
</select>
</section>
<div id="results" style="display: none;"></div>
</body>
</html>
Edit to add:
The modification to the JS that is shown below seems to be working and is understandable (at least to me). If there is further refinement possible I am all ears so to speak.
$(function() {
$("#ModSearch,#county").autocomplete({
source: function(request, response) {
var look4 = $("#ModSearch").val();
var county = $("#county").val();
var request = $.ajax({
url: 'SO_Question.php',
data: { look4 : look4 ,
county : county} ,
dataType: "text",
cache: false
})
request.done(function( response ) {
document.getElementById('results').innerHTML = response;
document.getElementById("results").style.display = "block";
});
}, // end of source
}); // end of autocomplete
}); // end of function