我在w3school here下面为手风琴实现了以下代码,并将其包装在表单标记中。当我尝试运行它时,我点击了手风琴标题,页面一直在刷新。怎么预防这个?以下是基于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
答案 0 :(得分:1)