js 中let一个网页,做ture 和false的选择
时间: 2024-12-15 11:23:31 浏览: 50
在JavaScript中,你可以创建一个简单的布尔选择(True or False)让用户在网页上进行选择。下面是一个基本的例子,使用HTML元素(如radio按钮)配合JavaScript来实现这个功能:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>True or False Choice</title>
<style>
input[type="radio"] {
margin-right: 10px;
}
</style>
</head>
<body>
<h2>Select a choice:</h2>
<form id="choiceForm">
<input type="radio" name="choice" value="true" id="trueChoice"> True<br>
<input type="radio" name="choice" value="false" id="falseChoice"> False<br>
<button onclick="checkChoice()">Submit</button>
</form>
<p id="result"></p>
<script>
function checkChoice() {
let userChoice = document.querySelector('input[name="choice"]:checked').value;
document.getElementById("result").textContent = "You chose: " + (userChoice ? 'True' : 'False');
}
</script>
</body>
</html>
```
在这个例子中,用户可以选择“True”或“False”。当他们点击提交按钮时,`checkChoice`函数会被触发,获取用户选择的radio按钮值,然后显示在页面上。
阅读全文
相关推荐

















