javascript - 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,并且还触发作为父页面的当前页面的“注销表单”。
解决方案
我认为可以做到这一点的唯一方法是:
- 转到发布到的服务器端代码
fwrdform
。 - 在文件的底部,添加一行代码,将页面重定向到
logout
表单所在的任何位置。
这是一个 PHP 示例:
echo "<script> window.location.href = 'logout.php'; </script>";
这是一种 NodeJS 方式:
document.write("<script> window.location.href = 'logout.php'; </script>");
然后在表单中添加另一行代码logout
,与上面的相同,只是它重定向回 HTML 页面 ( index.html
?)。
希望这可以帮助你!
推荐阅读
- griddb - 如何清理推文数据集以获得完美的情感分析词典?
- reactjs - 来自@storybook/react 的纱线审计原型污染
- android - 如果用户添加新项目,自动完成 Textview 会更新建议列表
- bash - 如何在 bash 脚本内的新终端中调用函数?
- linux - 我如何打开 eve-ng wizzard 设置
- c# - Windows 窗体按钮的按钮文本中的彩色字母表
- advanced-custom-fields - 在同一页面上的灵活内容块中调用 ACF 灵活内容块
- javascript - 内容安全政策指令不适用于电子
- dapper - Dapper GetList vs Query Method 具有不同的性能
- python - 如何在 Python 中组合列表和字典?