1.YII封装的CHtml::ajaxLink,QuoteController.php
quotes[array_rand($this->quotes, 1)]; } function actionIndex() { $this->render('index', array( 'quote' => $this->getRandomQuote() )); } function actionGetQuote() { $this->renderPartial('_quote', array( 'quote' => $this->getRandomQuote(), )); } }
view/index.php
Quote of the day
“”,clientScript->registerCoreScript('jquery'); ?> '#quote-of-the-day'))?>
ajaxLink($text,$url ,$ajaxOptions=array(),$htmlOptions=array())
$text 链接内容
以下是调试.也可以用常用的jQuery.ajax
'js:function(data){ alert(data); }'))?>
2.将php数据转化成javascript数据
protected/config/main.php:
'params'=>array( // this is used in contact page 'adminEmail'=>'[email protected]', 'alert' => array( 'enabled' => true, 'message' => 'Hello there!', ), ),
protected/controllers/AlertController.php
params->toArray()); //$config:{'adminEmail':'[email protected]','alert':{'enabled':true,'message':'Hello there!'}} Yii::app()->clientScript->registerScript('appConfig', "var config = ".$config.";",CClientScript::POS_HEAD); $this->render('index'); } }
registerScript第二个参数是显示js代码。
clientScript->registerScript('search', " $('.search-button').click(function(){ $('.search-form').toggle(); return false; }); $('.search-form form').submit(function(){ $.fn.yiiGridView.update('project-grid', { data: $(this).serialize() }); return false; }); "); ?>
protected/views/alert/index.php
CJSON::encode()=CJavaScript::encode 生成json格式
CJSON::decode()