JavaScript href onclick无法使用Perl

时间:2011-03-25 10:26:24

标签: javascript perl onclick href

以下是我的代码。我的所有按钮功能都运行正常,但是如果我点击链接,则应记住服务器的值,并且应该使用参数视图和子系统再次重新加载页面。

但是当我重新加载时,服务器的值是空的。

my $server = $q->param('server') ;
my $unit = $q->param('unit') ;

my $bfile = __FILE__ ;
$bfile = `basename $bfile` ;
chomp $bfile ;

print "<form name=\"form1\" action =\"/cgi-bin/$bfile\" onsubmit=\"javascript:onsubmitform(doc_press)\">";

print "<input type=\"hidden\" name=\"server\" id=\"server\">";
print "<input type=\"hidden\" name=\"unit\" id=\"unit\">";
print "\n\n<input type=submit name=view value=\"View\" onClick=\"doc_press=this.value\">";
print "<input type=submit name=save value=\"Save\" onClick=\"doc_press=this.value\">";

print $var{$a}."<a href=\"/cgi-bin/$bfile?view=5&SUBSYS=$subsys\" onClick=javascript:click_page(\"$_\")>CLICK</a>\n" ;

print <<"END_OF_SCRIPT";
<script type="text/javascript">
function onsubmitform(doc_press) {
        if (doc_press == "View"){
                document.getElementById('unit').value="$unit";
        }
        else if (doc_press == "Save") {
END_OF_SCRIPT
             var x = "$user=$val";
             document.cookie=x;
             document.getElementById('unit').value="$unit";
        }
        if (document.getElementById('HTTP_TYPE').value == "post") {
                document.form1.method='post';
        }
        else if(document.getElementById('HTTP_TYPE').value == "get") {
                document.form1.method='get';
        }
}

function click_page(server){
        document.getElementById('server').value=server;
}
</script>
END

1 个答案:

答案 0 :(得分:1)

当您点击链接(<a href="..."/>)时,浏览器将针对给定链接发出新的GET请求,无论您使用何种表单。这意味着您的表单未提交;所以表格中的任何价值都会丢失。出于这个原因,你在这里发布的onclick处理程序是没用的。

有时,如果您真的链接到同一页面,现代浏览器足够智能识别,并填写您已有的值。对于那些如果不保留其值而感到沮丧的用户来说,这只是一种商品,因此这对隐藏的字段不起作用。

如果您想要点击提交表单的链接,您应该a)使用按钮,或b)更改您的onclick处理程序以提交表单并返回false(以便不遵循链接):< / p>

function click_page(server){
    document.getElementById('server').value=server;
    document.forms[0].submit();
    return false;
}

要使其正常工作,还要更改onclick声明:

<a href="..." onclick="return click_page('$_')">CLICK</a>