简体   繁体   English

动态更改单选按钮时如何调用onclick函数?

[英]How to call an onclick function from a radio button when it is changed dynamically?

I'm working a CGI file and it gets the below parameter from query string and uses it as shown below. 我正在处理一个CGI文件,它从查询字符串中获取以下参数,并按如下所示使用它。

$rettype = p->param('rettype');
if($rettype eq "A")
{
$achecked="checked";
$rchecked="";
}
if($rettype eq "R")
{
$achecked="";
$rchecked="checked";
}

This is used in later portion of the file that chooses which radio button needs to be selected. 在文件的后面部分中使用此选项,该部分选择需要选择的单选按钮。

<INPUT TYPE="radio" name="gctyp" VALUE="a" $achecked onClick="showTextArea()";>Add
<INPUT TYPE="radio" name="gctyp" VALUE="r" $rchecked onClick="hideTextArea()";>Remove

Now, I want the onclick JS function present in the radio button to be called based on which option was checked. 现在,我希望根据选中的选项来调用单选按钮中存在的onclick JS函数。 It happens dynamically and the function isn't getting called. 它是动态发生的,并且不会被调用。

Thanks for your help 谢谢你的帮助

Seems like you are executing some Perl code and printing direct HTML to the output. 似乎您正在执行一些Perl代码并将直接HTML打印到输出中。 That's known as hardcoded HTML and is not a good practice. 那就是所谓的硬编码HTML,不是一个好习惯。 Look into templating modules like Template::Toolkit for better results and cleaner code. 查看诸如Template :: Toolkit之类的模板模块,以获得更好的结果和更简洁的代码。 Also, for your own good, I hope you are using use strict and use warnings at the top of your Perl code. 另外,为了您自己的利益,我希望您在Perl代码的顶部use strict use warningsuse warnings

Also, your HTML tags are written in uppercase. 另外,您的HTML标记是用大写形式编写的。 That's deprecated, use lowercase tags and parameters, and add / before closing tags that don't have a closing tag, just like input case. 不建议使用,使用小写标签和参数,并在关闭没有结束标签的标签之前添加/ ,就像input大小写一样。

That being said, this is an example of how to solve what you mentioned: 话虽如此,这是如何解决您提到的问题的一个示例:

my $javascript_call;

$rettype = p->param('rettype');
if ($rettype eq "A") {
    $achecked = "checked";
    $rchecked = "";
    $javascript_call = 'showTextArea()';
}
if ($rettype eq "R") {
    $achecked = "";
    $rchecked = "checked";
    $javascript_call = 'hideTextArea()';
}

print <<END;
    <input type="radio" name="gctyp" value="a" $achecked onclick="showTextArea()" />Add
    <input type="radio" name="gctyp" value="r" $rchecked onclick="hideTextArea()" />Remove
    <script type="text/javascript">
        $javascript_call
    </script>
END

I'm just adding an extra variable containing a call that will be executed after printing the radio buttons and using the same code structure you presented for that. 我只是添加一个包含调用的额外变量,该变量将在打印单选按钮并使用与您提供的代码结构相同的代码后执行。

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM