doudao1922 2016-11-30 21:04
浏览 313
已采纳

使用PHP解析数组-YII2中的JS变量解析JS文件中的PHP数组

How should I do this?

 $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);

I dont know how to parse this part:

$("#form").attr("action", "programas/" + "'.$ids[.'"valInt"'.].'");

$ids it's a php array

valInt is a js varibale

I thought this was correct but I'm getting parse error

syntax error, unexpected '.', expecting ']'

if I write "'.$ids[40].'" it works great and with no errors.

Thi is in the view of site/index.php, working with Yii2.

Thanks for the help!

  • 写回答

1条回答 默认 最新

  • dongqian9567 2016-11-30 21:53
    关注

    Try this

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理