joomla3.0 - How to change select list from another using AJAX in Joomla -
i have list of countries , list of cities each country. set both drop-down list.
my problem how can change cities listed when selected country changes?
this xml code:
<field name="country_id" type="sql" class="country" label="country" description="country" required="true" query="select id, title #__destination_countries state = 1" key_field="id" value_field="title" filter="raw"> </field> <field name="city_id" type="sql" query="select id, title #__destination_cities state = 1" key_field="id" value_field="title" class="city" label="city" description="city" required="true" filter="raw"> </field>
ajax code:
jquery(document).ready(function($) { $(".country").change(function(){ country_id = $("#" + this.id).val(); $.ajax({ url:'index.php?option=com_destination&task=cities.getlistcities', type: "post", data: { 'country_id': country_id } }).done(function(msg) { console.log(msg); $(".city").html(msg); }) }) });
this server-side code
public function getlistcities() { $country_id = jrequest::getint('country_id', 0); $model = $this->getmodel(); $model->setstate('filter.country_id', $country_id); $items = $model->getitems(); $option = array(); foreach ($items $key => $item) { $option[] = "<option value='{$item->id}'>{$item->title}</option>"; } $return = implode("", $option); echo $return; exit; }
but did not show want, show
<select style="display: none;" required="required" id="jform_city_id" name="jform[city_id]" class="city required chzn-done"> <option value="1">city 1 of coutry 1</option> </select> <div style="width: 220px;" class="chzn-container chzn-container-active" id="jform_city_id_chzn"> <a tabindex="-1" href="javascript:void(0)" class="chzn-single chzn-single-with-drop"> <span>city 1 of coutry 1</span> <div><b></b></div> </a> <div class="chzn-drop" style="display: block; width: 218px; top: 24px;"> <div class="chzn-search"><input tabindex="-1" style="width: 183px;" autocomplete="off" type="text"></div> <ul class="chzn-results"> <li id="jform_city_id_chzn_o_0" class="active-result" style="">city 1 of coutry 1</li> <li id="jform_city_id_chzn_o_1" class="active-result result-selected" style="">city 2 of coutry 1</li> </ul> </div> </div>
just change select option did not change below.
a note appart, you're using deprecated jrequest::getint();
should using
$input = jfactory::getapplication()->input; $country_id = $input->getint('country_id', 0);
in order change second select, must trigger update of second select:
$(".country").change(function(){ // ajax call code // on success, need trigger second select using $( ".city_id" ).val(your_value_from_ajax_response).trigger( "liszt:updated" ); });
Comments
Post a Comment