theInstallationForm.addEventListener("submit", function (event) {
event.preventDefault()
console.log(this.children)
const errors = []
for (let i = 0; i < this.children.length - 1; i ) {
console.log(this.children[i].value); // undefined
if (!this.children[i].value) {
errors.push(this.children[i].classList[1])
}
}
我在一個框中輸入一條訊息,但收到未定義的訊息。為什么??
uj5u.com熱心網友回復:
在您的表單中,您可能有一些元素.value在 JavaScript 中沒有該屬性。包括非輸入元素。您可以使用 CSS 選擇器來選擇具有以下value屬性的所有元素:
theInstallationForm.addEventListener("submit", function(event) {
event.preventDefault();
const errors = [];
for (var i = 0; i < this.querySelectorAll("input[value]").length - 1; i ) {
console.log(this.querySelectorAll("input[value]")[i].value);
if (!this.querySelectorAll("input[value]")[i].value) {
errors.push(this.querySelectorAll("input[value]")[i].classList[1]);
}
}
uj5u.com熱心網友回復:
i have made modivication to the formular , and the result of it is outputting positive results and ordinary expectations so far.
event.preventDefault()
console.log(this.children)
const errors = []
const children = this.querySelectorAll("input")
for (let i = 0; i < children.length - 1; i ) {
console.log(children[i].value);
if (!children[i].value) {
errors.push(children[i].classList[1])
}
}
if (errors.length) {
const els = document.querySelectorAll(".input");
for (let el of els) {
console.log(el.className)
if (errors.includes(el.className.split(" ")[1])) {
console.log("Required")
const span = el.querySelector("span");
span.innerText = "Required";
}
}
}
console.log(errors)
uj5u.com熱心網友回復:
installationForm.addEventListener("submit", function (event) {
event.preventDefault()
console.log(this.children)
const errors = []
const children = this.querySelectorAll("input")
for (let i = 0; i < children.length - 1; i ) {
console.log(children[i].value);
if (!children[i].value) {
errors.push(children[i].classList[1])
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/351782.html
標籤:javascript
上一篇:使用最近的D3版本從node.js生成PNG的最佳方法?
下一篇:ReactJs條件渲染不起作用
