简体   繁体   English

如何从Perl中的Submit按钮的onclick事件中调用Javascript函数?

[英]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')\\"

  1. If you use <form> you can use method="get" is easy way. 如果使用<form>,则可以使用method =“ get”是简单的方法。
  2. If you dont want to use <form> please add "id" into 如果您不想使用<form>,请在其中添加“ id”

<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.

相关问题 如何从javascript onclick()事件中调用php函数? - How do I call a php function from javascript onclick() event? 如何在“提交按钮的Onclick事件”中调用多个JavaScript函数? - How Can I Call Multiple JavaScript Functions In Onclick Event for Submit Button? 如何从onClick事件中调用对象函数? - How do I call an Object Function from an onClick Event? 如何在没有提交按钮的情况下从文本字段调用javascript函数? - How do i call a javascript function from a text field without submit button? 如何在提交按钮 onclick 事件中取消表单提交? - How do I cancel form submission in submit button onclick event? 从javascript onclick事件按钮调用c#函数 - call c# function from javascript onclick event button 如何在 onclick 事件完成后将“按钮”按钮更改为“提交”按钮 onclick 而不提交? - How do I change a “button” button to a “submit” button onclick without submitting after the onclick event is completed? 使用按钮 OnClick 事件调用 Javascript 函数 - Call Javascript function using button OnClick event 提交按钮点击事件mvc后如何调用javascript function - How to call javascript function after submit button click event mvc 单击“提交”按钮时如何调用Javascript函数? - How do I call a Javascript function when I click a “Submit” button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM