考虑到以下代码片段:
if('<?= Yii::app()->controller->action->id?>' == 'create'){
$("#Event_name").focusout(function(){
$.ajax({
success: function(html)
{
type: 'get',
url: '<?php echo $this->createUrl('related'); ?>'
这有效。但... 这是正确的做法吗?我希望一侧有 php,另一侧有 javascript,这有意义吗?
有人可以给我举一个例子,说明如果正确完成的话,这样的事情会是什么样子吗?
最佳答案
我对您正在使用的框架没有太多经验,但如果您将变量从 Controller 传递到您的 View ,那就更好了:
Controller :
...
$action_id = Yii::app()->controller->action->id;
$created_url = $this->createUrl('related'); //$this might not be in context here
$this->render('your_view_name', array('action_id'=>$action_id, 'created_url'=>$created_url));
...
查看:
if('<?= $action_id ?>' == 'create'){
$("#Event_name").focusout(function(){
$.ajax({
success: function(html)
{
type: 'get',
url: '<?= $created_url ?>'
关于php - 混合 php 和 javascript - 我们应该在这里使用 json 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11057722/