首页 > 解决方案 > document.getElementById onclick 也触发函数 2 / 表单 2

问题描述

谁能告诉我在哪里编码这个错误..

一旦用户通过 onclick 提交了 ID 为“fwrdform”的表单,我需要触发我的“注销表单”的附加功能,以便用户随后也退出站点。

<script>
document.getElementById('fwrdform').onclick = function() {
    document.getElementById('logout-form').submit();
};
</script>

它似乎不起作用,我无法理解为什么..

多谢你们!

我正在使用的完整编码是:

<a align="right" class="nav-link" href="http://EXAMPLE.COM/logout"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
<font color="#FFFFFF" style="font-size: 14px !important;">Logout</font>
</a>

<form id="logout-form" action="http://EXAMPLE.COM/logout" method="POST" style="display: none;">
</form>


 ////////////////////////////////////////////////


<form id="fwrdform" class="d-flex justify-content-left" method="post" action="http://EXAMPLE.COM/addtocart">

<input type="hidden" value="1" name="quantity" aria-label="Search" class="form-control" style="width: 65px">

<input type="hidden" name="product_id" value="{{$product->id}}">
<input type="hidden" width="100" height="40" value="submit" src="submit.png" alt="Submit">


<a href="http://EXAMPLE.COM/checkout" target="_blank" value="submit"><img border="0" src="http//EXAMPLE.COM/submit.png" width="100" height="auto">
</a>

目标是提交“fwrdform”表单,它将变量发送到新页面上的发票,该页面是不同的 url,并且还触发作为父页面的当前页面的“注销表单”。

标签: javascriptphphtmlcss

解决方案


我认为可以做到这一点的唯一方法是:

  1. 转到发布到的服务器端代码fwrdform
  2. 在文件的底部,添加一行代码,将页面重定向到logout表单所在的任何位置。

这是一个 PHP 示例:

echo "<script> window.location.href = 'logout.php'; </script>";

这是一种 NodeJS 方式:

document.write("<script> window.location.href = 'logout.php'; </script>");

然后在表单中添加另一行代码logout,与上面的相同,只是它重定向回 HTML 页面 ( index.html?)。

希望这可以帮助你!


推荐阅读