【发布时间】:2011-03-20 14:08:59
【问题描述】:
我正在逐步学习zend框架中的AJAX。我使用this question 作为第一步,并且在这个问题中接受的答案对我有用。现在我想使用 JSON 加载多个 DIV。这是我的计划。
IndexController.php:
class IndexController extends Zend_Controller_Action {
public function indexAction() { }
public function carAction() { }
public function bikeAction() { }
}
index.phtml:
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="js/ajax.js"></script>
<a href='http://practice.dev/index/car' class='ajax'>Car Image</a>
<a href='http://practice.dev/index/bike' class='ajax'>Bike Image</a>
<div id="title">Title comes here</div>
<div id="image">Image comes here</div>
car.phtml:
<?php
$jsonArray['title'] = "Car";
$jsonArray['image'] = "<img src='images/car.jpeg'>";
echo Zend_Json::encode($jsonArray);
?>
bike.phtml:
<?php
$jsonArray['title'] = "Bike";
$jsonArray['image'] = "<img src='images/bike.jpeg'>";
echo Zend_Json::encode($jsonArray);
?>
ajax.js:
jQuery(document).ready(function(){
jQuery('.ajax').click(function(event){
event.preventDefault();
// I just need a js code here that:
// load "Car" in title div and car2.jped in image div when "Car Image" link clicked
// load "Bike" in title div and bike2.jped in image div when "Bike Image" link clicked
});
});
我想你已经明白了。当单击任何带有 class='ajax' 的链接时,这意味着它的 AJAX 调用。 phtml 文件(car.phtml,bike.phtml)中数组(标题,图像)的索引显示应该在哪个 DIV 中加载此内容。
我的问题:
如果ajax.js 获取json 形式的数据,现在如何实现这个工作?
谢谢
【问题讨论】:
标签: ajax json zend-framework