Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 使用模板工具包勾选复选框而不使用多个循环_Html_Arrays_Perl_Checkbox_Template Toolkit - Fatal编程技术网

Html 使用模板工具包勾选复选框而不使用多个循环

Html 使用模板工具包勾选复选框而不使用多个循环,html,arrays,perl,checkbox,template-toolkit,Html,Arrays,Perl,Checkbox,Template Toolkit,我正在使用模板工具包和perl生成一个web页面。我有一个带有ID号的数组,如果数组中存在该值,它可以打开和关闭复选框。我想知道是否有更有效的方法在页面加载时打开和关闭一组复选框。有一些以前的代码,在我得到它之前,它感觉加载缓慢。我不想再增加等待时间了 这项工作: <label for="checkFedGround"> <input type="checkbox" name="Shipping" id="checkFedGround" value="11"

我正在使用模板工具包和perl生成一个web页面。我有一个带有ID号的数组,如果数组中存在该值,它可以打开和关闭复选框。我想知道是否有更有效的方法在页面加载时打开和关闭一组复选框。有一些以前的代码,在我得到它之前,它感觉加载缓慢。我不想再增加等待时间了

这项工作:

<label for="checkFedGround">
    <input type="checkbox" name="Shipping" id="checkFedGround" value="11"
        [% FOREACH ShippingID IN data.ShippingID %]
            [% IF ShippingID == 11 %] checked="checked" 
            [% END %]
        [% END %]
     enabled />             
        FedEx Ground
</label>

联邦快递
问题是,它必须为我的每种装运类型执行FOREACH循环。在我看来,会有很多不必要的处理。假设数组中有30个复选框和10个ID。这意味着FOREACH将对每个复选框循环10次,以验证单个ID

我曾经尝试使用TemplateToolkit::EXIST和类似的方法检查ID是否在数组中,但它没有按我希望的方式工作

这不起作用:勾选不在范围内的值的复选框

<input type="checkbox" name="Shipping" id="checkFedGround" value="11" 
    [% IF (data.ShippingID(11)) %] checked="checked"
[% END %]               
enabled />
FedEx Ground

联邦快递
有人知道更好的方法吗

更新/解决方案: 谢谢你的建议。正如建议的那样,我最终使用了散列来存储数据。我还放弃了使用静态复选框的想法,转而使用创建排序列表的选择列表。JQuery读取所选行/复选框的值,处理哈希数组并将其传递给Perl

<table> ...
    <td> <input type="checkbox" name="ShippingID" value="[% service.ShippingID %]"
        [% IF service.default %] checked="checked" [% END %]
    style="margin: 0px;" />
</td> </table>
....
<select id="ShippingMethodSelect">
    <option value="">Add Shipping Method...</option>
    [% FOREACH service IN data.Shipping %]
        <option value="[% service.ShippingID %]">[% service.description %]</option>
    [% END %]
</select> 
。。。
....
添加配送方式。。。
[%FOREACH服务在数据中。配送%]
[%service.description%]
[%END%]

然后不要使用数组,使用散列

$data->{ShippingIDs} = { map { $_ => 1 } @ShippingIDs };
然后你可以用

<input type="checkbox" name="Shipping" id="checkFedGround" value="11" 
    [% IF data.ShippingID.11 %] checked="checked" [% END %]
    enabled />
FedEx Ground

联邦快递

我喜欢使用HTML::FillInForm和Template::Toolkit来解决这类问题

use strict;
use warnings;
use Template::Toolkit;
use HTML::FillInForm;

my $template = qq(<label for="checkFedGround">FedEx Ground</label>
    <input type="checkbox" name="Shipping" id="checkFedGround" value="11" enabled />
);
my $params = {}; # Assuming you actually have some other work happening in TT
my $tt = Template::Toolkit->new;
$tt->process( $template, $params, \my $output );
my $form_data = { Shipping => [11,12] };
my $final_html = HTML::FillInForm->fill( \$output, $form_data );
使用严格;
使用警告;
使用模板::工具箱;
使用HTML::FillInForm;
my$template=qq(联邦快递地面)
);
我的$params={};#假设您在TT中实际有其他工作发生
my$tt=Template::Toolkit->new;
$tt->process($template,$params,\my$output);
我的$form_data={Shipping=>[11,12]};
我的$final\u html=html::FillInForm->fill(\$output,$form\u data);

(不确定语法,因为它是一个数字。您可能需要使用稍微不同的模板代码,但这个想法是正确的。)我最终按照建议使用了哈希。此外,还放弃了静态复选框的想法,转而使用选择列表创建一个已排序的复选框列表。