我已经看到了一些与我的问题相关的答案,但这些答案并没有帮助我得到结果。 我正在使用 zend Framework 1.12,我必须实现来自两个不同表的依赖下拉表单元素。
我的表单元素:
$country = new Zend_Form_Element_Select('Country');
$country -> setLabel('Country');
$city = new Zend_Form_Element_Select('city');
$city -> setLabel('City:');
$this->addElements(array($country,$city));
国家/地区表是城市表的父表,使用 Zend_Dbtable 和 jQuery。
有人可以告诉我如何使用 jQuery 和 AJAX 来做到这一点吗?我的意思是从 Controller 到 View 。
最佳答案
从 Controller 加载表单时,您可以从 Controller 设置国家/地区下拉值,如下所示:
$form->get('countries')->setAttributes(array(
'options' => $country_list,
));
并在ajax中基于选择更改(国家/地区),进行ajax调用以加载城市:
$("select[name='countries']").change(function(){
var sel_country = this.value;
$.ajax({
type: 'POST',
url: '{mention your url to get the cities for chosen country}',
data: { country: sel_country },
success:function(data){
// Assign the cities to select drop-down.
$("select[name='cities']").html(data);
}
});
});
其他:
您在表单加载期间知道所选的国家/地区,获取所选国家/地区的城市并进行分配,
$form->get('cities')->setAttributes(array(
'options' => $city_list,
));
其他:
分配到国家/地区列表和选择城市列表以查看变量并使用 View 文件中的 View 变量,并使用 foreach 语句为国家/地区和城市下拉列表分配选项值(选择下拉列表)。
我希望这会有所帮助。
关于javascript - 如何使用ajax和JQuery实现依赖下拉zend框架1.12表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20295101/