gpt4 book ai didi

javascript - querySelector 似乎正在从输入 id "#submit"和另一个输入类型 ="submit"识别 ="submit"

转载 作者:行者123 更新时间:2023-12-02 22:22:58 26 4
gpt4 key购买 nike

我创建了一个函数来在两个元素的 style.display 之间切换为“none”或“block”。我在这里遇到的问题是“发送”按钮似乎与“切换”按钮具有相同的行为。这不是我指定它要做的事情。我想知道这是否与输入中存在 type="submit"有关。

let binary = document.querySelector('#submit')
binary.addEventListener('click', toggler);


function toggler() {
let phaseTwo = document.querySelector('.phasetwo');
let phaseOne = document.querySelector('.phaseone');


if (phaseOne.style.display == "none") {
phaseTwo.style.display = "none";
phaseOne.style.display = "block";
} else {

phaseTwo.style.display = "block";
phaseOne.style.display = "none";

}


}
.phaseone {
display: block;
}

.phasetwo {
display: none;
}
<!DOCTYPE html>
<html>
<head>
<title>html</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="container">
<div class="phaseone">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="phasetwo">
<form id="moveConfig"class="config">
Red: <br>
<input type="text" name="" placeholder="5000"> ms <br>
Yellow: <br>
<input type="text" name="" placeholder="3000"> ms <br>
Green: <br>
<input type="text" name="" placeholder="5000"> ms <br>
<input id="send" type="submit" value="send">
</form>

</div>
<input id="submit" type="submit" name="" value="toggle">
</div>
<script type="text/javascript" src="app.js"></script>
</body>
</html>

最佳答案

是的,“相同行为”的原因是发送按钮上的属性 type="submit"

它实际上所做的是提交表单并刷新它。

修复方法:

  1. 将类型设置为type=button并通过JS处理按钮点击。按钮类型默认不会提交表单。
  2. 删除表单元素(不是最佳实践),因为提交类型通常适用于表单。

这是更新后的代码片段:

let binary = document.querySelector('#submit')
binary.addEventListener('click', toggler);


function toggler() {
let phaseTwo = document.querySelector('.phasetwo');
let phaseOne = document.querySelector('.phaseone');


if (phaseOne.style.display == "none") {
phaseTwo.style.display = "none";
phaseOne.style.display = "block";
} else {

phaseTwo.style.display = "block";
phaseOne.style.display = "none";

}


}
.phaseone {
display: block;
}

.phasetwo {
display: none;
}
<!DOCTYPE html>
<html>
<head>
<title>html</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="container">
<div class="phaseone">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="phasetwo">
<form id="moveConfig"class="config">
Red: <br>
<input type="text" name="" placeholder="5000"> ms <br>
Yellow: <br>
<input type="text" name="" placeholder="3000"> ms <br>
Green: <br>
<input type="text" name="" placeholder="5000"> ms <br>
<input id="send" type="button" value="send">
</form>

</div>
<input id="submit" type="button" name="" value="toggle">
</div>
<script type="text/javascript" src="app.js"></script>
</body>
</html>

关于javascript - querySelector 似乎正在从输入 id "#submit"和另一个输入类型 ="submit"识别 ="submit",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59163438/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com