小编Pha*_*ang的帖子

如何在 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> 
Run Code Online (Sandbox Code Playgroud)

阿贾克斯代码:

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

        })
    })
});
Run Code Online (Sandbox Code Playgroud)

这是我的服务器端代码

public function getListCities()
{
    $country_id = JRequest::getInt('country_id', 0);
    $model …
Run Code Online (Sandbox Code Playgroud)

joomla3.0

4
推荐指数
1
解决办法
5699
查看次数

标签 统计

joomla3.0 ×1