Perl CGI 和 Perl JavaScript |选择选项的烦恼
我很难弄清楚为什么以下代码不起作用:
test.cgi:
#!/usr/bin/perl -w
use CGI;
$cgi = new CGI;
$cgi->autoEscape(undef);
%ptype = ("0","","1","Pennsylvania","2","New York","3","Ohio");
print $cgi->header('text/html'),
$cgi->start_html(-title=>'Test',-script=>[{-type=>'javascript',-src =>'/scripts/test.js'}]),
$cgi->start_form(-method=>'GET',id=>'frmMain',-name=>'frmMain',-enctype=>'multipart/form-data'),
$cgi->popup_menu(-style=>'width:200',name=>'ProblemType',-values=>\%ptype,-onChange=>'PopulateSType()',-default=>'0'),
$cgi->popup_menu(-style=>'width:200',name=>'SubProblemType',-values=>''),
$cgi->end_form,
$cgi->end_html();
test.js:
function PopulateSType() {
var ProblemList = document.frmMain.ProblemType;
ClearOptions(document.frmMain.SubProblemType);
if (ProblemList[ProblemType.selectedIndex].value == "1") {
AddToOptionList(document.frmMain.SubProblemType, "0", "");
AddToOptionList(document.frmMain.SubProblemType, "1", "Pittsburgh");
AddToOptionList(document.frmMain.SubProblemType, "2", "Philadelphia");
AddToOptionList(document.frmMain.SubProblemType, "3", "Harrisburg");
}
if (ProblemList[ProblemType.selectedIndex].value == "2") {
AddToOptionList(document.frmMain.SubProblemType, "0", "");
AddToOptionList(document.frmMain.SubProblemType, "1", "New York");
AddToOptionList(document.frmMain.SubProblemType, "2", "Buffalo");
AddToOptionList(document.frmMain.SubProblemType, "3", "Middletown");
}
if (ProblemList[ProblemType.selectedIndex].value == "3") {
AddToOptionList(document.frmMain.SubProblemType, "1", "Cleveland");
AddToOptionList(document.frmMain.SubProblemType, "2", "Cincinatti");
AddToOptionList(document.frmMain.SubProblemType, "3", "Akron");
}
}
function ClearOptions(OptionList) {
for (x = OptionList.length; x >= 0; x = x - 1) {
OptionList[x] = null;
}
}
function AddToOptionList(OptionList, OptionValue, OptionText) {
OptionList[OptionList.length] = new Option(OptionText, OptionValue);
}
Sample source output:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"><head><title>Process Alarm</title>
<script language="JavaScript" src="/scripts/test.js" type="javascript"></script>
</head><body><form method="get" action="/cgi-bin/test.cgi/test.cgi?null" enctype="multipart/form-data" name="frmMain" id="frmMain">
<select name="ProblemType" onchange="PopulateSType()" style="width:200">
<option value="1">Pennsylvania</option>
<option value="3">Ohio</option>
<option selected="selected" value="0"></option>
<option value="2">New York</option>
</select><select name="SubProblemType" style="width:200">
<option value=""></option>
</select></form></body></html>
Everything看起来应该可以找到,但是当我加载页面时,没有任何反应第二个选择选项按钮。如果页面加载时应用宽度样式,则似乎是命中或失败。我什至在 test.js 的顶部尝试过 window.onload = load;
。我看到的唯一可能有问题的是 perl 将 onChange 格式化为 onchange。
java 在常规 HTML 中工作得很好,只是在尝试在 perl 中实现它时似乎有问题。我使用此处中的示例
I'm having a tough time trying to figure out why the following code isn't working:
test.cgi:
#!/usr/bin/perl -w
use CGI;
$cgi = new CGI;
$cgi->autoEscape(undef);
%ptype = ("0","","1","Pennsylvania","2","New York","3","Ohio");
print $cgi->header('text/html'),
$cgi->start_html(-title=>'Test',-script=>[{-type=>'javascript',-src =>'/scripts/test.js'}]),
$cgi->start_form(-method=>'GET',id=>'frmMain',-name=>'frmMain',-enctype=>'multipart/form-data'),
$cgi->popup_menu(-style=>'width:200',name=>'ProblemType',-values=>\%ptype,-onChange=>'PopulateSType()',-default=>'0'),
$cgi->popup_menu(-style=>'width:200',name=>'SubProblemType',-values=>''),
$cgi->end_form,
$cgi->end_html();
test.js:
function PopulateSType() {
var ProblemList = document.frmMain.ProblemType;
ClearOptions(document.frmMain.SubProblemType);
if (ProblemList[ProblemType.selectedIndex].value == "1") {
AddToOptionList(document.frmMain.SubProblemType, "0", "");
AddToOptionList(document.frmMain.SubProblemType, "1", "Pittsburgh");
AddToOptionList(document.frmMain.SubProblemType, "2", "Philadelphia");
AddToOptionList(document.frmMain.SubProblemType, "3", "Harrisburg");
}
if (ProblemList[ProblemType.selectedIndex].value == "2") {
AddToOptionList(document.frmMain.SubProblemType, "0", "");
AddToOptionList(document.frmMain.SubProblemType, "1", "New York");
AddToOptionList(document.frmMain.SubProblemType, "2", "Buffalo");
AddToOptionList(document.frmMain.SubProblemType, "3", "Middletown");
}
if (ProblemList[ProblemType.selectedIndex].value == "3") {
AddToOptionList(document.frmMain.SubProblemType, "1", "Cleveland");
AddToOptionList(document.frmMain.SubProblemType, "2", "Cincinatti");
AddToOptionList(document.frmMain.SubProblemType, "3", "Akron");
}
}
function ClearOptions(OptionList) {
for (x = OptionList.length; x >= 0; x = x - 1) {
OptionList[x] = null;
}
}
function AddToOptionList(OptionList, OptionValue, OptionText) {
OptionList[OptionList.length] = new Option(OptionText, OptionValue);
}
Sample source output:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"><head><title>Process Alarm</title>
<script language="JavaScript" src="/scripts/test.js" type="javascript"></script>
</head><body><form method="get" action="/cgi-bin/test.cgi/test.cgi?null" enctype="multipart/form-data" name="frmMain" id="frmMain">
<select name="ProblemType" onchange="PopulateSType()" style="width:200">
<option value="1">Pennsylvania</option>
<option value="3">Ohio</option>
<option selected="selected" value="0"></option>
<option value="2">New York</option>
</select><select name="SubProblemType" style="width:200">
<option value=""></option>
</select></form></body></html>
Everything looks like it should work find, however when I load the page nothing happens with the second select option button. It seems hit or miss if the width style applies when the page loads. I've even tried window.onload = load;
at the top of test.js. The only thing that I am seeing that may be amiss is perl is formatting onChange as onchange.
The java works fine in regular HTML, it just seems to have issues when trying to implement this in perl. I'm using an example from here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它应该是
type="text/javascript"
,浏览器支持的 JS MIME 媒体类型。type="javascript"
本身不会被识别。 (language="javascript"
已过时。)应为
200px
。不确定
null
是否能保证正常工作。传统的快速习语是:It should be
type="text/javascript"
, the MIME media type for JS that browsers support.type="javascript"
on its own won't be recognised. (language="javascript"
is obsolete.)should be
200px
.Not sure
null
is guaranteed to work. The traditional quick idiom is: