Yii2 $this-gt;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 的值动态化,就像我要将 params1 从 php 变量传递给 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 = yiihelpersUrl::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);
当然,您也可以使参数的数量动态化,因为它只是一个传递给 Url::to().
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 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!