Perl JavaScript/Variable 从 JavaScript/jquery 传递到 Perl

perl javascript/variable passing from javascript/jquery to perl

本文关键字:Perl JavaScript Variable jquery      更新时间:2023-09-26
<input type="checkbox" id = "has_sidebar" name="cb" value="" onclick="result=$(this).attr('value', this.checked ? 1 : 0); alert('my result : 'result.context.value);" >

RESULT.CONTEXT.VALUE具有复选框的值(01 )。

如何在 perl 变量中分配它?
这是代码。

<%class>
use JSON;
use URI::Escape;
has 'data';
has 'cb'        => ( isa => 'Array');
</%class>
<%init>
my $args = eval {
        return from_json(uri_unescape($.data), { ascii => 1});
};
$m->redirect('/login') unless $USER && $USER->{'logged_in'};
$args->{'authors'} = [] unless $args->{'authors'} && ref($args->{'authors'}) eq 'ARRAY';
</%init>
<li class="header">
        <p>
                Authors
                &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
                &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
                show sidebar
        </p>
</li>
%  my @author_box = ();
% foreach my $a (sort { $a->{'last_name'} cmp $b->{'last_name'} || $a->{'first_name'} cmp $b->{'first_name'} } @{$args->{'authors'}}) {
<li>
        <p>
                <a href="javascript:void(0)" onclick="remove_author(<% $a->{'id'} %>);" class="right icon-small icon-delete-small"></a>
                <% $a->{'last_name'} %>, <% $a->{'first_name'} %> (<% $a->{'initials'} %>)
                &emsp;&emsp;&emsp;&emsp;
                &emsp;&emsp;&emsp;&emsp;
                <input type="checkbox" id = "has_sidebar" name="cb" value="" onclick="result=$(this).attr('value', this.checked ? 1 : 0); alert('id : <% $a->{'id'} %> - checked: ' + result.context.value);" >
        </p>
</li>
% # put the value of the checkbox in array
<script type="text/javascript">
<% $.cb %> = result.context.value  //this is not the RIGHT WAY!!!
var hsb = [];
hsb.push({ <% $a->{'id'} %>: <% $.cb %> });
</script>
% }
<li>
        <p>
                <input type="submit" value="Add" class="right" onclick="add_author();"/>
                <input id="new_author" style="width:75%;" />
                <input type="hidden" id="new_author_data" />
        </p>
</li>

据我理解你的问题:

你不能直接将值从javascript传递给perl,因为perl是在SERVER-SIDE执行的(它构建html+javascript),然后javascript运行CLIENT-SIDE。你可以从perl->javascript传递值,但反之则不然。

您需要表单提交或 AJAX 调用,然后在服务器端进行处理。

工作流程:

  1. 服务器端处理(在您运行Perl的情况下) - 构建页面
  2. 交付给客户
  3. 客户端的东西 - 运行JavaScript
  4. 通过表单或 AJAX 请求将结果返回给服务器
  5. 处理结果(再次运行一些 perl)

编辑:

关于表单处理和AJAX的所有内容已经在互联网和Stackoverflow上写了数百次。

您需要阅读以下内容:FormProcessing 和 AJAX。

我还建议您查看jquery文档,因为jquery有一个非常强大的ajax套件,可以为您节省大量工作。

使用 Perl 进行表单处理

另一个使用 Perl 进行表单处理的初学者协会

jQuery Ajax 部分

jQuery Ajax-Call

或者只是谷歌并阅读你喜欢的图特。