我应该怎么做?
$ids = ArrayHelper::map(Tours::find()->select(['pk', 'programa'])->asArray()->all(), 'pk', 'programa');
$this->registerJs('
$("#child1_child2").change(function() {
var val = $("#child1_child2 option:selected").val();
valInt = parseInt(val);
$("#form").attr("action", "programas/" + "'.$ids[.'"valInt"'.].'");
});
', \yii\web\View::POS_END);
我不知道如何解析这部分:
$("#form").attr("action", "programas/" + "'.$ids[.'"valInt"'.].'");
$ids 这是一个 php 数组
valInt 是一个 js 变量
我认为这是正确的,但我遇到了解析错误
syntax error, unexpected '.', expecting ']'
如果我写 "'.$ids[40].'"
它工作得很好并且没有错误。
这是在 site/index.php 的 View 中,与 Yii2 一起使用。
感谢您的帮助!
最佳答案
试试这个
$ids = ArrayHelper::map(Tours::find()->select(['pk', 'programa'])->asArray()->all(), 'pk', 'programa');
$this->registerJs('
$("#child1_child2").change(function() {
var val = $("#child1_child2 option:selected").val();
valInt = parseInt(val);
var $ids = '. json_encode($ids) .';
$("#form").attr("action", "programas/" + $ids[valInt]);
});
', \yii\web\View::POS_END);
关于javascript - 解析 JS 文件中的 PHP 数组,并在 PHP 解析数组中包含 JS 变量 -YII2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40898155/