json_encode}';

问题描述 投票:0回答:1
{include file="second.tpl"}

You can achieve this by making

<!-- first.tpl -->
<script>
   ``` 
    $(document).ready(function() {
        objIns.loadNames = '{$names|json_encode}';
     }
    )
   ```
</script>
{include file="second.tpl"}
<!-- end of first.tpl -->

<!-- second.tpl -->
some code here
<script>
```
$(document).ready(function() {
    objIns.loadNames = '{$names|json_encode}';
})
```
</script> 

objIns as a global variable.Declaring variables inside the function will make there scope local, so declare variables outside the function so that it can be used globally.

javascript jquery
1个回答
0
投票

in fileAdd these lines inside document ready function

<script>
var objIns;
$(document).ready(function() {
objIns.loadNames = '{$names|json_encode}';
</script>
{include file="second.tpl"}

In first.tpl file

Access storagedata in second.tpl

0
投票

clickobjIns variable before $(document).ready function in first.tpl file as global

var objIns='';

我有以下的代码结构。

我的问题是,为什么我需要声明变量 javascript localstorageobjIns.loadNamesfirst.tpl 在second.tpl文件中,因为它已经在first.tpl文件中加载了?有什么办法可以不在second.tpl中重新声明这个变量。我试过用 "var "关键字来声明,但是没有用。

objIns.loadNames = '{$names|json_encode}';
localStorage.setItem("objIns",objIns.loadNames);

我的代码结构如下。

localStorage.getItem("objIns");

$(document).ready(function() { objIns.loadNames = '{$names'}。

© www.soinside.com 2019 - 2024. All rights reserved.