Yii2 $ this-> registerJs($ js);如何在$ js内传递php变量 [英] Yii2 $this->registerJs($js); How to pass php variable inside the $js
本文介绍了Yii2 $ this-> registerJs($ js);如何在$ js内传递php变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我认为的ajax脚本.
Below is my ajax script in my view.
$js = <<< JS
$('.list-link').click(function(){
$.ajax({
url: '?r=public/getlist¶m1=01¶m2=02¶m3=03',
dataType: "json",
success: function(data) {
$(".well").html(data.id);
}
})
});
JS;
$this->registerJs($js);
现在我的问题是我该如何使param1,param2和param3的值动态化,就像我要通过php变量将params1传递给3一样.
Now my problem is how am I going to make the values of param1, param2 and param3 dynamic like I am going to pass the params1 to 3 from php variables.
推荐答案
您可以这样做:
$url = \yii\helpers\Url::to([
'public/getlist',
'param1' => '01',
'param2' => '02',
'param3' => '03'
]);
$js = <<< JS
$('.list-link').click(function(){
$.ajax({
url: $url,
dataType: "json",
success: function(data) {
$(".well").html(data.id);
}
})
});
JS;
$this->registerJs($js);
Of course you can make the number of parameters dynamic as well since it is just an array that gets passed to Url::to().
有关使用的 Heredoc (允许变量使用)语法的官方信息,可以找到
Official info about the used Heredoc (which allows variable usage) syntax can be found here.
这篇关于Yii2 $ this-> registerJs($ js);如何在$ js内传递php变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文