javascript - 如何将文字参数传递给包含的js?
问题描述
如何将文字参数传递{$currchoice.id}
给js?我有以下货币选择器,我需要将其选择的值作为参数传递给(currency=2)
javascript(currency={$currchoice.id})
<!-- Currency -->
{if !$loggedin && count($currencies) > 1}
<div class="pull-right nav">
<a href="#" class="quick-nav" data-toggle="popover" id="currencyChooser"><i class="fa fa-usd"></i> {$LANG.choosecurrency} <span class="caret"></span></a>
<div id="currencyChooserContent" class="hidden">
{foreach from=$currencies item=currchoice}
<img src="{$BASE_PATH_IMG}/flags/{$currchoice.code|substr:0:2|strtolower}.png"> <a href="{$currentpagelinkback}currency={$currchoice.id}">{if $currency.id eq $currchoice.id}<u>{/if}{$currchoice.code}</u></a>
{/foreach}
</div>
</div>
{/if}
<script language="javascript" src="feeds/productsinfo.php?pid=1&get=price&billingcycle=monthly¤cy=2"></script>
解决方案
您似乎有一个popover
用户可以选择货币的菜单,然后您根据用户的选择询问如何添加另一个脚本。
好吧,如果您更喜欢刷新页面,只需将 URL 参数从控制器传递到视图,例如:
public function getMyView($request) {
// TODO: replace 0 with your default currency-id.
return view('path.to.view')
->with('selectedCurrency', $request->input('currency') ?? 0)
}
并在视图中使用它,例如:
<script language="javascript" src="feeds/productsinfo.php?pid=1&get=price&billingcycle=monthly¤cy={{$selectedCurrency}}"></script>
但是,如果您不想在每次用户点击时刷新,那么:
- 从您的视野中完全删除该
<script ...
事物。 - 添加
JQuery
代码(或类似的代码)以查找选定的值(例如货币 ID)。 - 最后,
script
使用JavaScript
.
请注意,第二种方法需要您的整个 JS(以及依赖于它的东西)来支持延迟加载(不会导致错误)。
推荐阅读
- excel - 用于数字过滤器的 MS Excel VBA 宏脚本中的错误
- json - 在 JQ 中省略 sub() 的空值
- reactjs - 组件定义缺少显示名称 - React.js
- ajax - 使用 HtmlUnit 发出 Ajax 请求
- c# - 为什么 While 循环在 C# XAML 中不起作用?
- python - 如何为以下游戏添加“重玩”选项
- javascript - 页面重新加载后用户投票时保持按钮禁用
- c# - 寻找一些用于研发的术语
- amazon-web-services - Elastic Beanstalk 未创建 ssl.conf
- sql - 从 Postgres 记录中的行中获取最大值并按多列分组