1

我的控制器是

public function init()
{
    /* Initialize action controller here */
    $ajaxContext = $this->_helper->getHelper('AjaxContext');
    $ajaxContext->addActionContext('get-ajax-content', 'html')
                ->initContext();
}

public function indexAction()
{
    // action body
    $form = new Zend_Form_Element_Select('menu');
    $parent_array=range('1','9');
        $form->addMultiOptions(array($parent_array));
        $this->view->form = $form;
}

对于这个表单元素,我正在调用 ajax 函数,

<script type="text/javascript">

  $(document).ready(function(){
    $("#menu").change(function(){
        $.ajax({
            type: "POST",
            url: "get-data",
            data: "val="+$(this).val(),
            success: function(result){
                $("#div_sortorder").html(result);
            },
            error: function(){
                //console.log('error');
            },
            complete: function(){
                //console.log('complete');
            }
        });
    });
 </script>

并且获取数据操作如下

public function getDataAction()
{
    // action body
    $this->view->message = 'This is an ajax message!';
    $params = array(    'host'      =>'localhost',
                        'username'  =>'root',
                        'password'  =>'',
                        'dbname'    =>'cms'
                       );
    $DB = new Zend_Db_Adapter_Pdo_Mysql($params);
    $section_id=$this->getRequest()->getPost('val');
    $menu_order='SELECT * FROM `cms_content_mst` WHERE section_id='.$section_id;
    $menu_order=$DB->fetchAll($menu_order);
    $newContent='<select id="sortorder" name="sortorder">';
    if(!empty($section_id) || $section_id != 0){
        if (empty($menu_order)) {
                    $newContent.='<option value="1" selected="selected">1</option>';
        }
        else
        {

        for ($i=1; $i <= count($menu_order) ; $i++) { 

                  $newContent.='<option value="'.$i.'"';
                  if($i==count($menu_order))
                  { $newContent.=' selected'; }
                  $newContent.='>'.$i.'</option>';

            }

        }
      }
      $newContent.='</select>';

    $this->view->new = $newContent;

}

我已经完全工作了 jquery 。我有 .get 功能,但在调用 .ajax 时它不起作用我是 zend plz 的新手,帮助

4

1 回答 1

0

请!如果您能够加载页面,请在控制台中检查。

 $.ajax({
            type: "POST",
            url: "get-data",
            data: "val="+$(this).val(),
            success: function(result){
                $("#div_sortorder").html(result);
            }

我认为您提供了错误的网址。

于 2012-11-01T09:24:23.657 回答