JavaScript href onclick 不适用于 Perl

发布于 2024-10-26 17:04:48 字数 1612 浏览 1 评论 0原文

下面是我的代码。我的所有按钮功能都工作正常,但是如果我单击链接,则应记住服务器的值,并且应使用参数 view 和 subsys 再次重新加载页面。

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

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

The below is my code. All my button functions are working perfectly, but if I click the link click the value of server should be remembered and the page should be reloaded again with the parameters view and subsys.

But the value for the server is empty when I getting reloaded.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2024-11-02 17:04:48

当您单击链接 () 时,浏览器将为给定链接发出新的 GET 请求,无论您拥有何种形式。这意味着您的表格尚未提交;因此表单中的任何值都将丢失。因此,此处发布的 onclick 处理程序毫无用处。

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

如果您想单击链接来提交表单,您应该 a) 使用按钮,或者 b) 更改 onclick 处理程序以提交表单并返回 false(以便不跟踪链接)

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

:正确的话,还要更改 onclick 声明:

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

When you click on a link (<a href="..."/>), the browser will make a new GET request for the given link, regardless of any forms you might have. This means that your form is NOT submitted; so any value in the form will be lost. For this reason, your onclick handler as posted here is useless.

Sometimes, if you’re really linking to the same page, modern browsers are smart enough to recognize that, and fill in the values you already had. This is only a commodity to users who get frustrated if their values are not kept, and so this doesn't work for hidden fields.

If you want clicking on the link to submit the form, you should either a) use a button, or b) change your onclick handler to submit the form and return false (so that the link isn’t followed):

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

To make this work correctly, also change the onclick declaration:

<a href="..." onclick="return click_page('$_')">CLICK</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文