【问题标题】:Executing perl code only on clicking <a> tag仅在单击 <a> 标记时执行 perl 代码
【发布时间】:2020-06-15 18:08:33
【问题描述】:

我只想在单击&lt;a&gt; 标记时执行一些 perl 代码,而不是每次加载 .html.ep 文件时,我不知道该怎么做。 到目前为止,我尝试将 perl 代码放在 href 前面:

<a <% $type = $type eq 'desc' ? 'asc' : 'desc'; %>
                      href="<%= url_for('customer_statistics')
                          . "?sort_by=" . "date_begin"
                          . "&type=" . $type %>">

但它不起作用,因为它在每次页面加载时都在执行。 我的问题是:我可以只在点击&lt;a&gt;标签时执行perl代码,而不使用javascript吗?

【问题讨论】:

  • 你不能在客户端执行 Perl 代码——浏览器不理解 Perl——所以我假设你希望它在服务器上执行。这意味着客户端将需要向服务器发出请求。这意味着您需要&lt;a href="https://..."&gt;...&lt;/a&gt;,其中的 URL 会导致 Perl 代码在服务器上执行。这当然会用该 Perl 代码的输出替换正在查看的页面。如果您不想打开它,则必须使用 JS(发出 AJAX 请求)。
  • 看起来 Op 正在使用 metacpan.org/pod/distribution/HTML-EP/lib/HTML/EP.pod,所以它已经作为 CGI 脚本运行了。

标签: html perl


【解决方案1】:

我通过使用第二个变量 $next_type 解决了这个问题:

<th><a <% $next_type = $type eq 'desc' ? 'asc' : 'desc'; %>
                      href="<%= url_for('customer_statistics')
                          . "?sort_by=" . "num_comments"
                          . "&type=" . $next_type %>">
                        <%= l('No. comments') %></a>

比使用 JS 更优雅

【讨论】:

    猜你喜欢
    • 2011-09-29
    • 2023-03-06
    • 2014-01-02
    • 1970-01-01
    • 2021-10-09
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多