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 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Yii2 $this->registerJs($js);如何在 $js 中传递 php 变量
基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
