我正在研究 JavaScript 并测试一个我认为可以阻止选中复选框的函数(代码示例):
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" charset="utf-8">
var myBox = document.getElementById('bike');
myBox.addEventListener('click', stopCheck, false);
function stopCheck(e){
e.preventDefault();
}
</script>
<form action="">
<input type="checkbox" name="vehicle" value="Bike" id="bike" >I have a bike<br>
<input type="checkbox" name="vehicle" value="Car">I have a car
</form>
</body>
</html>
但代码不起作用。可能在复选框更改选中状态后执行 stopCheck 。如何解决?