Javascript 有谁能告诉我为什么在这个代码中按钮是不可点击的,以及如何使它可点击?
我有两个html文本输入字段和一个按钮。第一个将获取id,第二个将获取密码,单击按钮将显示一个div,表示欢迎,或者显示一个段落,询问正确的登录凭据。id和密码是硬编码的。 问题是提交按钮根本不可点击。我不知道是什么导致了这次失败,请帮忙Javascript 有谁能告诉我为什么在这个代码中按钮是不可点击的,以及如何使它可点击?,javascript,html,onclick,submit,Javascript,Html,Onclick,Submit,我有两个html文本输入字段和一个按钮。第一个将获取id,第二个将获取密码,单击按钮将显示一个div,表示欢迎,或者显示一个段落,询问正确的登录凭据。id和密码是硬编码的。 问题是提交按钮根本不可点击。我不知道是什么导致了这次失败,请帮忙 <input type="text" class="id" style="margin-top:10px"> <input type="text" class=
<input type="text" class="id" style="margin-top:10px">
<input type="text" class="pass" style="margin-top:10px">
<button class="enter" style="margin-top:6px; padding:6px; background:orange;">Enter</button>
<p class="loginMsg"></p>
<script>
const idvalue ="Superhero";
const passvalue ="Superzero";
const id = document.querySelector(".id");
const pass = document.querySelector(".pass");
const enter = document.querySelector(".enter");
const para = document.querySelector(".loginMsg");
enter.onclick = function() {
let myid = id.value;
let mypass = pass.value;
if(myid==idvalue && mypass==passvalue) {
let panel = document.createElement("div");
let msgpara = document.createElement("p");
msgpara.textContent="Welcome back "+idvalue;
let btn = document.createElement("button");
btn.textContent = "OK";
panel.appendChild(msgpara);
panel.appendChild(btn);
html.appendChild(panel);
btn.onclick = function(){
panel.parentNode.removeChild(panel);
};
};
else {
para.textContent = "Please enter correct id and password.";
};
};
</script>
进入
const idvalue=“超级英雄”;
const passvalue=“Superzero”;
const id=document.querySelector(“.id”);
const pass=document.querySelector(“.pass”);
常量enter=document.querySelector(“.enter”);
const para=document.querySelector(“.loginMsg”);
enter.onclick=function(){
设myid=id.value;
让mypass=pass.value;
if(myid==idvalue&&mypass==passvalue){
让panel=document.createElement(“div”);
让msgpara=document.createElement(“p”);
msgpara.textContent=“欢迎回来”+idvalue;
设btn=document.createElement(“按钮”);
btn.textContent=“确定”;
儿童问题小组(msgpara);
面板.附件(btn);
appendChild(面板);
btn.onclick=函数(){
panel.parentNode.removeChild(panel);
};
};
否则{
para.textContent=“请输入正确的id和密码。”;
};
};
检查第33行的代码,您在if-else语句中的else之前添加了分号
if{
...};
else{
};
有关更正,请参阅。是,因为在创建问题的if()条件之后使用分号(;)将其删除,它将正常工作
enter.onclick = function() {
let myid = id.value;
let mypass = pass.value;
if(myid==idvalue && mypass==passvalue) {
let panel = document.createElement("div");
let msgpara = document.createElement("p");
msgpara.textContent="Welcome back "+idvalue;
let btn = document.createElement("button");
btn.textContent = "OK";
panel.appendChild(msgpara);
panel.appendChild(btn);
html.appendChild(panel);
btn.onclick = function(){
panel.parentNode.removeChild(panel);
};
} else {
para.textContent = "Please enter correct id and password.";
};
};
在您的手机上安装一个门楣,谢谢您的回复。除此之外,我忘了定义html元素,这也导致面板无法显示。谢谢你的帮助。