-2

我必须依赖组合框

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

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

4

2 回答 2

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
于 2012-04-19T18:37:37.747 回答
0

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

于 2012-04-19T10:41:21.027 回答