如何将文字参数传递给包含的js?

How to pass literal parameter to included js?

如何给js传{$currchoice.id}这样的字面参数? 我有以下货币选择器,我需要将其选择的值作为 (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&currency=2"></script>

您似乎有一个 popover 菜单,用户可以在其中 select 货币,然后您询问如何根据用户的选择添加另一个脚本。

好吧,如果你喜欢刷新页面,只需将 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&currency={{$selectedCurrency}}"></script>

但是如果您不想在每次用户点击时刷新,那么:

  • 从您的视图中完全删除 <script ... 东西。
  • 添加 JQuery 代码(或类似代码)以查找 selected 值(例如货币 ID)。
  • 然后最后,使用 JavaScript.
  • 动态添加 script-元素

Note that the second approach requires your entire JS (and what depends on it) to support lazy loading (without causing errors).