我的控制器是
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 的新手,帮助