微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

joomla3.0 – 如何在Joomla中使用AJAX更改另一个选择列表

我有一个国家列表和每个国家的城市列表.我将它们都设置为下拉列表.

我的问题是,当所选国家/地区发生变化时,如何更改列出的城市?

这是我的XML代码

<field name="country_id" type="sql" class="country" label="Country"
       description="Country" required="true"
       query="SELECT id,title FROM #__destination_countries WHERE state = 1"
       key_field="id" value_field="title" filter="raw">
</field> 

<field name="city_id" type="sql"
       query="SELECT id,title FROM #__destination_cities WHERE state = 1"
       key_field="id" value_field="title" class="city" label="City"
       description="City" required="true" filter="raw">
</field>

Ajax代码

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);

        })
    })
});

这是我的服务器端代码

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 as $key => $item) {
        $option[] = "<option value='{$item->id}'>{$item->title}</option>";
    }
    $return = implode("",$option);
    echo $return;
    exit;
}

但它并没有像我想的那样显示,它表现得像这样

<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>

只需更改选择选项,但不会更改以下内容.

解决方法

注意appart,你正在使用已弃用的JRequest :: getInt();你应该在哪里使用

$input = JFactory::getApplication()->input;
$country_id = $input->getInt('country_id',0);

要更改第二个选择,您必须触发第二个选择的更新:

$(".country").change(function(){
    // your ajax call from your code

    // on success,you need to trigger the second select by using
    $( ".city_id" ).val(your_value_from_ajax_response).trigger( "liszt:updated" );

});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐