在表单设计中,使用开关(通常指复选框或单选按钮)来控制输入框的显示或隐藏,是一种常见的交互设计模式。这种设计可以让用户根据需要选择性地填写表单信息,提高用户体验和表单的灵活性。
以下是一个使用HTML和JavaScript实现开关返回输入框的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Switch Input Example</title>
<style>
.hidden {
display: none;
}
</style>
</head>
<body>
<form>
<label>
<input type="checkbox" id="switch">
Enable Additional Information
</label>
<div id="additionalInfo" class="hidden">
<label for="additionalInput">Additional Input:</label>
<input type="text" id="additionalInput">
</div>
</form>
<script>
document.getElementById('switch').addEventListener('change', function() {
const additionalInfo = document.getElementById('additionalInfo');
if (this.checked) {
additionalInfo.classList.remove('hidden');
} else {
additionalInfo.classList.add('hidden');
}
});
</script>
</body>
</html>
disabled
属性被设置为true
。disabled
属性没有被设置为true
。通过以上方法,可以有效地解决在使用开关返回输入框时可能遇到的问题。
领取专属 10元无门槛券
手把手带您无忧上云