【问题标题】:How can I pass smarty variable to .js file for var definition?如何将 smarty 变量传递给 .js 文件以进行 var 定义?
【发布时间】:2019-01-13 10:23:46
【问题描述】:

我在 php 中从服务器请求获取参数 id 的 id

$id = $_REQUEST['id'];
$smarty->assign('id', $id);

并且在 smarty .tpl 显示文件中 {$id} 将数字输出到客户端浏览器。

我在页面的 javascript 中需要这个变量: var id = {$id};

我稍后在 javascript 中使用 id 来定义数据表的默认搜索值。

"oSearch": {"sSearch": offer_id},

我的问题是我需要正确地将 id 传递给 javascript,如果设置的话。现在它显示 {$id}。请问有什么想法吗?

【问题讨论】:

  • 您的 JavaScript 在您的 .tpl 文件或 .js 文件中?

标签: javascript file variables smarty assign


【解决方案1】:

在您的模板 (.tpl) 文件中,包含代码

<script>
  var id = {$id};
  console.log(id); // for debugging purposes
</script>

在浏览器的 Javascript 控制台中,您应该能够看到 id 的值。

【讨论】:

    猜你喜欢
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    相关资源
    最近更新 更多