我有两个文件index.html和index.js。当我填写表单中的文本字段并单击按钮时,它应该重定向到index.js。我怎样才能做到这一点?
index.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>标记本身内创建函数,然后在<button>标记中调用onClick中的函数。但是,我希望onClick调用我的index.js脚本,它将执行后端内容
发布于 2020-10-30 05:51:20
在index.js中声明此函数
function handleClick() {
if (
document.getElementById('email').nodeValue ===
document.getElementById('pass').nodeValue
) {
alert('You are allowed');
}
}按一下按钮调用它
<button onclick="handleClick()">Click</button>发布于 2020-10-30 05:51:27
应该使用以下方法将html文件链接到javascript文件
<script type="text/javascript" src="(your file location)"></script>然后添加事件侦听器以侦听按钮,单击
document.addEventListener('DOMContentLoaded', function () {
document.getElementById("button-id").addEventListener('click', yourFunction)
});
function yourFunction(){
//your code here
}还向按钮添加一个id,这样您就可以将事件侦听器添加到该按钮中。
<button id="button-id">Click</button>发布于 2020-10-30 05:56:27
您需要使用EventListener将按钮单击事件绑定到函数。
document.getElementsByTagName('button')[0].addEventListener('click',function(){
if (document.getElementById("email").value==document.getElementById("pass").value){
alert("You are allowed");
}
});<!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>
</body>
</html>
https://stackoverflow.com/questions/64603230
复制相似问题