首页 > 解决方案 > 如何将文字参数传递给包含的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&currency=2"></script>

标签: javascriptphpliterals

解决方案


您似乎有一个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&currency={{$selectedCurrency}}"></script>

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

  • 从您的视野中完全删除该<script ...事物。
  • 添加JQuery代码(或类似的代码)以查找选定的值(例如货币 ID)。
  • 最后,script使用JavaScript.

请注意,第二种方法需要您的整个 JS(以及依赖于它的东西)来支持延迟加载(不会导致错误)。


推荐阅读