I've trawled Stackoverflow and the web in general looking for an answer to this. I've found some good suggestions (e.g http://af-design.com/blog/2010/05/12/using-jquery-uis-autocomplete-to-populate-a-form/) but can't get any to work ... completely due to my ignorance!
I have a JSON file containing a Locality, State and Postcode data (shortened version):
[
{
"PCODE":7255,
"LOCALITY":"LOCCOTA",
"STATE":"TAS"
},
{
"PCODE":7255,
"LOCALITY":"LUGHRATA",
"STATE":"TAS"
},
{
"PCODE":7255,
"LOCALITY":"MEMANA",
"STATE":"TAS"
}
]
Basically I want to allow a user to enter a Locality into a form field and then have jQuery search the JSON file, find a match for Postcode and State and use those matching values to populate Postcode and State form text fields
Here's the form I'm using plus some test jQuery pulled from http://af-design.com/ (which I can't get to work - my fault, not source script):
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<style>
label{
float:left;
width:80px;
}
</style>
<link rel="stylesheet" href="http://static.jquery.com/ui/css/base2.css" type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
var ac_config = {
source: "p-codes.json",
select: function(event, ui){
$("#city").val(ui.item.LOCALITY);
$("#state").val(ui.item.STATE);
$("#zip").val(ui.item.PCODE);
},
minLength:1
};
$("#city").autocomplete(ac_config);
});
</script>
</head>
<body>
<form action="#" method="post">
<p><label for="city">City</label><br />
<input type="text" name="city" id="city" value="" /></p>
<p><label for="state">State</label><br />
<input type="text" name="state" id="state" value="" /></p>
<p><label for="zip">Zip</label><br />
<input type="text" name="zip" id="zip" value="" /></p>
</form>
</body>
</html>
Any help or suggestions would be much appreciated!
Regards,
Mekong
Should'nt it be like this ?
http://jqueryui.com/autocomplete/#remote
OK ... a bit more trawling around the web and I was able to sort this one out, thanks to http://www.jensbits.com/ All working now.