提交表单+ zendFramework时,选项不会再次添加

时间:2012-04-18 10:21:34

标签: php zend-framework jquery combobox zend-form

我必须依赖组合框

 $this->addElement('Select', 'Category',array(
         'label'      => 'Category:',
         'AutoComplete'=> true,              
         'multiOptions' => array('0' => '-Category-',$a->GetCategories(),'2' => '-Add             category-'),
         'required' => true ));

  $this->addElement('Select', 'SubCategory',array(
          'label'      => 'Sub Category:',
          'AutoComplete'=> true, 
          //'multiOptions' => array('0' => '-Select Category-'),
          'required' => true ));

使用ajax填充第二个

<script type="text/javascript">
  //for send data i'll use jquery library
  $(document).ready( function(){
    $('#Category').change(function() {
      var message=$('#Category option:selected').text();
      if (message != '') {
        $.ajax({
          type: "GET",
          dataType : 'json',
          url: 'http://localhost/EverTags1/Authentification1/public/Product/add', 
          async: false,
          data:{"message" : message}, 
          success: function (respond) {
            var json=JSON.stringify(respond);
            var  objet = eval('(' + json + ')');
            e=objet.length;
            var str = "";
            for ( var count = 0 ; count < e; count++ ) { 
              str += "<option value='" + count + "'>" + objet[count].name+ "</option>"
            } 
            $("#SubCategory").empty().append(""+str);
          }
        }); 
      }
    });
  });
</script>

元素在第二个组合框中正确加载。但是当我提交第二个组合框的内容消失时。如何让它们显示

2 个答案:

答案 0 :(得分:1)

您需要在每次ajax请求后更新多重选项。我用会话来做那个

public function getsubcategoriesAction()
{
   if($this->_request->isXmlHttpRequest())
    {
        $session = new Zend_Session_Namespace('mySession');
        $this->getRequest()->param('id',1)
        $model = new Application_Model_DbTable_Subcategory();
        $result = $model->getSubcategories($category);
        // save the result to session
        $session->result = $result;
        $this->_helper->json($result);

    }
}

并在呈现表单的操作中

 public function createAction()
 { 
    //some code here

    if($this->getRequest()->isPost()){
        $session = new Zend_Session_Namespace('mySession');
        $subCategory = $form->getelement('subCategory');
        $subCategory->addMultiOptions($session->result); // get the result back from session

      //some code here
   }
}

您还需要在application.ini中启用会话

 resources.session.save_path = APPLICATION_PATH "/../data/session"
 resources.session.use_only_cookies = true
 resources.session.remember_me_seconds = 864000

答案 1 :(得分:0)

selected='selected'属性添加到#SubCategory的第一个选项是否会修复它?