加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 02:45:46 所属栏目:百科 来源:网络整理
导读:我有一个国家列表和每个国家的城市列表.我将它们都设置为下拉列表. 我的问题是,当所选国家/地区发生变化时,如何更改列出的城市? 这是我的XML代码: field name="country_id" type="sql" class="country" label="Country" description="Country" required="t
我有一个国家列表和每个国家的城市列表.我将它们都设置为下拉列表.

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

这是我的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" );

});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读