gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:11:15 24 4
gpt4 key购买 nike

我有一个国家列表和每个国家的城市列表。我将两者都设置为下拉列表。

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

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

只需更改选择选项,但没有更改以下内容。

最佳答案

除了注意,您使用的是已弃用的 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");

});

关于joomla3.0 - 如何在 Joomla 中使用 AJAX 从另一个更改选择列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15383822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com