[英]How do I call a Javascript Function from onclick event of submit button in Perl?
I am trying to fire a javascript by onclick event of submit button but not able to, Details of my code are "i have a button named filter and two text boxes which take the Id and Name, All i want is "When i enter the value in Id textbox and click Filter then i want the values to be displayed on URL using QueryString". here's the code.. 我试图通过“提交”按钮的onclick事件触发一个javascript,但无法执行,我的代码详细信息是“我有一个名为filter的按钮和两个带有ID和名称的文本框,我想要的是“当我输入Id文本框中输入值,然后单击“过滤器”,然后我希望使用QueryString在URL上显示这些值。这是代码。
print "<td><b>UserId</b></td><td><input type=\"text\" name=\"User_Id\"
value=\"" .$Id."\" size=\"6\" ></td>";
print "<td><b>UserName</b></td><td><input type=\"text\" name=\"User_Name\"
value=\"" .$Name ."\" size=\"10\"></td>";
print "<td><input type=\"submit\" name=\"Filter\" value=\"Filter\"
onClick=\"FilterExpression($Id,$Name)\"></td>";
After i click Filter this code gets executed.. 单击筛选后,此代码将被执行。
if ( $q->param("Filter") )
{
$Id=$q->param('User_Id');
$Name=$q->param('User_Name');
if ($Id ne "" )
{
$filterexpression= $filterexpression." UserId like '" .$Id. "%' and " ;
}
if ($Name ne "" )
{
$filterexpression= $filterexpression." UserName like '" .$Name. "%' and " ;
}
}
The Javascript.. Javascript ..
<script type="text/javascript">
function FilterExpression(Id,Name)
{
var val3=Id;
var val4=Name
window.location="List.cgi?Id="+val3+"&Name="+val4
}
</script>
Please Do help me out find the solution,Thank you. 请帮我找出解决方案,谢谢。
Looks like you may need quotes. 看起来您可能需要引号。 Try onClick=\\"FilterExpression('$Id','$Name')\\"
尝试onClick=\\"FilterExpression('$Id','$Name')\\"
<input type=\\"text\\" id='User_Id' name=\\"User_Id\\" value=\\"" .$Id."\\" size=\\"6\\" > <input type = \\“ text \\” id ='User_Id'name = \\“ User_Id \\” value = \\“”。$ Id。“ \\” size = \\“ 6 \\”>
and write Javascript like this. 并像这样编写Javascript。
function FilterExpression() { var val3=document.getElementById("User_Id").value; var val4=document.getElementById("User_Name").value; window.location="List.cgi?Id="+val3+"&Name="+val4 }
Your problem comes from trying to do complicated and hard to manage quotes and escapes. 您的问题来自试图执行复杂且难以管理的引号和转义符。
If you ever have to escape quotes in a Perl program, chances are, you are doing it wrong. 如果您不得不在Perl程序中转义引号,则很可能您做错了。
Perl has many different ways to quote strings that make it easy to manage strings, and fill-in variable values. Perl有许多不同的方式来引用字符串 ,从而使管理字符串和填充变量值变得容易。 The powerful quoting operators make escaping quote characters an extreme rarity. 强大的引号运算符使转义引号字符极为罕见。
I'll show you a few examples. 我将向您展示一些示例。
Your example could be handled with an interpolating here-doc : 您的示例可以通过插值here-doc来处理:
my $filter_expression = FilterExpression($Id,$Name);
print <<"END_HTML";
<td><b>UserId</b></td><td><input type="text" name="User_Id" value="$Id" size=\"6\" ></td>"
<td><b>UserName</b></td><td><input type="text" name="User_Name" value="$Name" size="10"></td>
<td><input type="submit" name="Filter" value="Filter" onClick="$filter_expression"></td>
END_HTML
Or you could use the qq
operator to quote assemble your output: 或者您可以使用qq
运算符来引用汇编您的输出:
print qq{<td><b>UserId</b></td><td><input type="text" name="User_Id" value="$Id" size="6" ></td>};
print qq[<td><b>UserName</b></td><td><input type="text" name="User_Name" value="$Name" size="10"></td>];
print qq(<td><input type="submit" name="Filter" value="Filter" onClick=."$filter_expression"></td>);
Or if you insist on avoiding interpolation, simply use a single quote : 或者,如果您坚持要避免插值,只需使用单引号 :
print '<td><b>UserId</b></td><td><input type="text" name="User_Id" value="'
.$Id
.'" size=\"6\" ></td>';
print '<td><b>UserName</b></td><td><input type="text" name="User_Name" value="'
.$Name
.'" size=\"10\"></td>';
print '<td><input type="submit" name="Filter" value="Filter" onClick="'
.FilterExpression($Id,$Name)
.'"></td>';
Also, seriously consider using a template system to handle your HTML generation. 另外,请认真考虑使用模板系统来处理HTML生成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.