javascript - 如何同时测试表单上的所有输入?
问题描述
当我测试我的输入并将整个表单或两个条目留空时,我只会得到第一个没有用 notValid 类添加填充的输入。我认为这将同时测试所有内容,但只有输入才能获得新课程。任何帮助,将不胜感激。
btn.addEventListener('click', () => {
if (title.value.length == 0) {
title.classList.add('notValid');
e.preventDefault();
}
if (author.value.length == 0) {
author.classList.add('notValid');
e.preventDefault();
}
if (pages.value.length == 0) {
pages.classList.add('notValid');
e.preventDefault();
}
});
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Library</title>
</head>
<body>
<button type="text" id='addBookBtn' class='addBookBtn'>Add Books</button>
<div class="book-container" id='book-container'>
<h1>add a book</h1>
<label for="read">Read?</label>
<input type="checkbox" class='readInput' id='readInput'>
<label for="title">Title:</label>
<input type="text" class='title' id='title' placeholder='Add a Title'>
<label for="author">Author:</label>
<input type="text" class='author' id='author' placeholder='Add an Author'>
<label for="pages">Pages:</label>
<input type="number" class='pages' id='pages' placeholder=' Number of Pages'>
<button class='btn' id='btn' type="submit">Add</button>
</div>
<div class='newBookContainer' id='newBookContainer'></div>
<script src="script.js"></script>
</body>
</html>
解决方案
您忘记在 eventListener 中包含事件参数
btn.addEventListener('click', (e) => {}
您可以做的另一件事是避免手动检查输入的各个值,您可以通过添加相似的类名来循环所有输入字段,或者只查询表单内的所有输入字段。此外,拥有如此多的输入,这是将它们添加到内部的好方法form
// optional: add the input class for all inputs
<input type="checkbox" class="input readInput" id="readInput" />
const form = document.getElementById('form');
const inputs = form.querySelectorAll('input'); // or query the classnames you've given for all input fields
form.addEventListener('submit', (e) => {
e.preventDefault();
// or simply use Array.from(inputs).forEach()
// calling Array.prototype.slice.call will convert NodeLists into Array
Array.prototype.slice.call(inputs).forEach((el) => {
if (el.value.length === 0) {
el.classList.add('notValid');
} else {
el.classList.remove('notValid');
}
})
});
.notValid {
border: 1px solid red;
}
<button type="text" id='addBookBtn' class='addBookBtn'>Add Books</button>
<div class="book-container" id='book-container'>
<form id="form">
<h1>add a book</h1>
<label for="read">Read?</label>
<input type="checkbox" class='input readInput' id='readInput'>
<label for="title">Title:</label>
<input type="text" class='input title' id='title' placeholder='Add a Title'>
<label for="author">Author:</label>
<input type="text" class='input author' id='author' placeholder='Add an Author'>
<label for="pages">Pages:</label>
<input type="number" class='input pages' id='pages' placeholder=' Number of Pages'>
<button class='btn' id='btn' type="submit">Add</button>
</form>
</div>
推荐阅读
- r - 如何让所有 30 分钟的标签中断显示在 x 轴上?
- html - 如何使用 Nokogiri 获取没有类名或 ID 或任何属性的 DIV?
- python - 无法在 R w/ Reticulate 中访问 Anaconda 环境
- r - rvest 包在 mabcook 和 ubuntu 上给出不同的结果
- php - How to fix Prestashop front office, 500 internal server error?
- python - AJAX,POST返回方法不允许
- java - 黑客可以更改模型属性吗?春天,Java
- c# - 如何递归使用 Leaf.xnet 子字符串
- git - 解决冲突的最佳方法是什么?变基还是合并?
- python - 列表中的“分组”项目