gpt4 book ai didi

javascript - 单击时, Accordion 标题会刷新页面

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

我从 w3school here 实现了以下 Accordion 代码段并将其包装在 form 标记中。当我尝试运行它时,我单击了 Accordion 标题,页面不断刷新。如何防止这种情况发生?下面是我基于w3school的代码。

<form name="formone" method="post" action="file.php">
<button onclick="myFunction('Demo1')" class="w3-btn w3-block w3-black w3-left-align">Vehicle</button>
<div id="Demo1" class="w3-container w3-hide">
<input type="checkbox" name="vehicle" value="Bike"> Bike<br>
<input type="checkbox" name="car" value="Car"> Car<br>
</div>
<button onclick="myFunction('Demo2')" class="w3-btn w3-block w3-black w3-left-align">Distance</button>
<div id="Demo2" class="w3-container w3-hide">
<input type="checkbox" name="distance" value="m"> Metre<br>
<input type="checkbox" name="distance" value="km"> KM<br>
</div>
<br>
<button type="submit" name="submit">Submit</button>
</form>

</div>

<script>
function myFunction(id) {
var x = document.getElementById(id);
if (x.className.indexOf("w3-show") == -1) {
x.className += " w3-show";
} else {
x.className = x.className.replace(" w3-show", "");
}
}
</script>

这是我的 JSFiddle

最佳答案

您必须在按钮元素中给出类型标记以指定它不是提交按钮

<button type="button">

https://jsfiddle.net/e3raaywm/1/

关于javascript - 单击时, Accordion 标题会刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48248456/

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