javascript - 如何让 onClick() 从我的 javascript 文件中读取?
问题描述
我有两个文件index.html
和index.js
. 当我填写表单中的文本字段并单击按钮时,它应该重定向到index.js
. 我该如何做到这一点?
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1 id="head">Hello</h1>
<input type="email" id="email"></input>
<br><br>
<input type="password" id="pass"></input>
<br><br>
<button>Click</button>
<script src="index.js"></script>
</body>
</html>
index.js
if (document.getElementById("email").nodeValue==document.getElementById("pass").nodeValue){
alert("You are allowed");
}
编辑:我可以简单地通过在<script>
标签本身内创建函数然后onClick
在<button>
标签内调用函数来做到这一点。但是相反,我想onClick
调用我的index.js
脚本来执行后端的东西
解决方案
在 index.js 中声明这个函数
function handleClick() {
if (
document.getElementById('email').nodeValue ===
document.getElementById('pass').nodeValue
) {
alert('You are allowed');
}
}
点击按钮调用它
<button onclick="handleClick()">Click</button>
推荐阅读
- android - Fire Stick 4k - ADB 隐藏更新(禁用更新)权限错误
- reactjs - 如何使 Webpack 5 在我的 Docker 容器中可用?
- python - Python使用特定键将所有字典值设置为小写
- html - 如何对齐复选框长标签?
- javascript - 如何将其他选项中的相同值发布到服务器
- flutter - 如何隐藏 pdf 查看器插件颤动中存在的导航栏
- c# - ASP.NET Core IOptions - 是否可以从生产环境中的环境变量填充配置 POCO 敏感字段?
- typescript - Rollup.js 不会在包中包含节点模块 discord-rpc。有没有办法做到这一点?
- automation - 我想测试提交表单,但单击提交按钮会调用来自赛普拉斯不同域的 api
- asp.net-core - 通用存储库模式中的当前用户(ASP.NET 身份)信息