tp5.0 模板url跳转带参数 字符串中js不解析变量

来源:互联网 发布:日本房产中介 知乎 编辑:程序博客网 时间:2024/06/08 15:29
<input id="my-input" type="hidden" value="1" />
var type = $("#my-input").val();
var str = '{:u("test/test/preview",array("type"=>'+type+'))}';
$("iframe").attr('src',st);

结果为:
src="/dedao/index.php?g=test&m=test&a=preview&type=%2Btype%2B"
为什么结果不是:
src="/dedao/index.php?g=test&m=test&a=preview&type=1"
收起
alert(type);

结果为:

/dedao/index.php?g=test&m=test&a=preview&type=%2Btype%2B

我们可以发现,这个type没有作为js变量解析,而是还是把它当作字符串当作url地址,双引号经过urlencode解析,我在百度了好多以后还是没解决问题,不过经过多次实验以后,发现是因为花括号{}, 这个{}里面js变量是不能被js识别的,我也不知道为什么。

为了解决这个问题,我将url传参地址模式改了,不用带有{}的,换成 /../test/test/preview?type='+type+'.html 这样就可以了,至于为什么{}不能识别,我再去查查