【问题标题】:How to pass literal parameter to included js?如何将文字参数传递给包含的js?
【发布时间】:2021-10-16 22:11:49
【问题描述】:

如何将像{$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>

【问题讨论】:

  • 当您将2 替换为{$currchoice.id} 时会发生什么?
  • 我试过但没有效果
  • “没有效果”真的没有什么意义。查看编译后的源代码,看看它在那里显示的内容。
  • &lt;script language="javascript" src="feeds/productsinfo.php?pid=1&amp;get=price&amp;billingcycle=monthly&amp;currency={$currchoice.id}"&gt;&lt;/script&gt; 结果`“belocloud.com/feeds/…”`&lt;script language="javascript" src="feeds/productsinfo.php?pid=1&amp;get=price&amp;billingcycle=monthly&amp;currency={$currchoice.id}"&gt;&lt;/script&gt; 结果即使它的 MIME 类型 (“text/html”) 不是有效的 JavaScript MIME 类型。`

标签: javascript php literals


【解决方案1】:

您似乎有一个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>

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

  • &lt;script ... 完全从您的视图中移除。
  • 添加 JQuery 代码(或类似代码)以查找所选值(例如货币 ID)。
  • 最后,使用JavaScript动态添加script-元素。

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

【讨论】:

    猜你喜欢
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多