【问题标题】:How to implement Dependent drop down zend framework 1.12 form using ajax and JQuery如何使用ajax和JQuery实现依赖下拉zend框架1.12表单
【发布时间】:2013-11-30 00:25:20
【问题描述】:

我已经看到了一些与我的问题相关的答案,但这些答案并没有帮助我获得结果。 我正在使用 zend 框架 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));

Country Table 是 City Table 的父表,使用 Zend_Dbtable 和 jQuery。

谁能告诉我如何使用 jQuery 和 AJAX 来做到这一点。我的意思是从控制器到视图。

【问题讨论】:

  • 在此处查看示例:remysharp.com/2007/01/20/…
  • 这不是我的问题。你知道 Zend 框架吗?你已经发布了普通的 php 示例,我知道该怎么做。

标签: javascript jquery ajax zend-framework zend-form-element


【解决方案1】:

从控制器加载表单时,您可以从控制器中设置国家/地区下拉值,例如:

 $form->get('countries')->setAttributes(array(
    'options' => $country_list,
 ));

在ajax中根据select change(Country),进行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,
 ));

其他:

分配给国家列表和选择城市列表以查看变量并在视图文件中使用视图变量,并使用 foreach 语句将选项值分配给国家和城市的下拉列表(选择下拉列表)。

我希望这会有所帮助。

【讨论】:

  • 返回的json数据是这样的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-23
  • 1970-01-01
  • 2015-05-18
  • 1970-01-01
  • 2019-10-25
  • 1970-01-01
相关资源
最近更新 更多