如何使用Perl CGI弹出菜单打开新页面

问题描述 投票:1回答:1

我希望通过下面的Perl-cgi脚本在菜单中选择一个项目时打开一个新页面;就像我们对window.open所做的那样。如果有人对如何执行类似于以下命令的想法有所了解:window.open('http://www.google.com','_blank','toolbar = 0,location = 0,menubar = 0');

先谢谢您

#!/usr/bin/perl -Tw
use strict;
use warnings;
use utf8;
use CGI ;
my $cgi = new CGI;
my %url = (
    "https://www.google.com"    => "google" ,
    "https://www.msn.com"       => "msn",
    "https://www.yahoo.com"     => "yahoo" 
);
print $cgi->header("text/html;charset=UTF-8");
print "<!DOCTYPE html>";
print "<html>\n";
print "<head>\n";
print "</head>\n";
print "<body>\n";
print '<form>'."\n";
print $cgi->popup_menu(
  - name     => 'url',
  - id       => 'url',
  - values   => [sort keys %url],
  - default  => ['google'],
  - labels   => \%url,
  - onchange => 'submit();'
    #- onchange => "this.form.submit();"
);
print "</form>\n";
print "</body>\n";
print "</html>\n";
javascript perl cgi window.open
1个回答
1
投票

您几乎拥有它,但您可能想要this.value

this.value

但是正如我评论的那样,由于这些CGI方法已被弃用,请务必考虑使用诸如print $cgi->popup_menu( - name => 'url', - id => 'url', - values => [sort keys %url], - default => ['google'], - labels => \%url, - onchange => q{window.open(this.value, '_blank', 'toolbar=0,location=0,menubar=0');}, ); 之类的替代方法。 TT中的常见模式是有两个可能如下所示的文件。

Perl文件:

Template::Toolkit

模板文件:

#!/usr/bin/perl -Tw

# my_form.cgi

use strict;
use warnings;
use utf8;
use HTTP::Headers;
use Template;

my $headers = HTTP::Headers->new;
$headers->header('Content-Type' =>  'text/html;charset=UTF-8');
print $headers->as_string;

my %urls = (
    "https://www.google.com"    => "google" ,
    "https://www.msn.com"       => "msn",
    "https://www.yahoo.com"     => "yahoo" 
);

my $template = Template->new;
$template->process('my_form.ttml', { urls => \%urls });

您可以查看[%# my_form.ttml %] <!DOCTYPE html><html> <head> </head> <body> <form> <select name="url" id="url" onchange="window.open(this.value, '_blank', 'toolbar=0,location=0,menubar=0');"> [% FOREACH url IN urls -%] <option value="[% url.key %]">[% url.value %]</option> [% END -%] </select> </form> </body> </html> Template::Manual以获得有关Template :: Toolkit的详细信息,或者您可以查看Template::Manual::Intro以获得更多启发。

© www.soinside.com 2019 - 2024. All rights reserved.