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


Options not getting added again when the form is submitted +zendFramework

>I 必须依赖组合框

 $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>

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

您需要

在每个 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
   }
}

您还需要在应用程序中启用会话.ini

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

selected='selected' 属性添加到第一个选项 #SubCategory 修复它吗?