autocomplete - Jquery-ui Auto complete not working properly -
i'm trying display auto complete text box. i'm trying display city names , wanna store corresponding cityid
of city in box while selecting 1 city.the auto complete box working fine(i mean i'm getting city name list).but when select city, name not showing in box shows id. code given below.
<div class="col-sm-10"> <input type="text" name="txtcity" placeholder="city" id="txtcity" class="form-control"> <input type="text" name="txtcityid" placeholder="city" id="txtcityid" class="form-control"> </div> <script type="text/javascript"> $(function() { $("#txtcity").autocomplete({ source: 'getcitylist', select: function (event, ui) { $("#txtcity").html(ui.item.label); // display selected text // $("#txtcityid").val(ui.item.value); // save selected id hidden input } }); }); </script>
this data base function:
public function getcitylist(){ $term = input::get('term'); $citylist = citymodel::getcitynameajax($term); foreach ($citylist $key=>$value) { $data[$key]['label']=$value->cityname; $data[$key]['value']=$value->id; } echo json_encode($data); }
you can prevent default methods of setting value event.preventdefault()
or can return false
also.
complete code:
$( "#textcity" ).autocomplete({ source: atags, select: function (event, ui) { event.preventdefault(); //preventing default methods $("#textcity").val(ui.item.label); // display selected text $("#textcityid").val(ui.item.value); // save selected id hidden input } });
working demo link: http://jsfiddle.net/9r4cv/806/
Comments
Post a Comment