JS将表单数据获取到数组中,使用foreach循环遍历数组获取的值为undefined。

huangapple go评论109阅读模式
英文:

JS get form data into array foreach loop for array getting values as undefined

问题

我从一个表单中获取值,然后运行一个循环来检查它们是否为空。如果为空,我将缺少信息的文本框设为红色,否则设为绿色。然而,当我将我的值放入循环中时,它们变成了未定义值。我已经检查了填写表单后数组中的值,它们是存在的。有人知道为什么会发生这种情况。

  1. let Ac = document.forms["formtest"]["Accession"].value;
  2. let ItName = document.forms["formtest"]["ItemName"].value;
  3. let date = document.forms["formtest"]["date"].value;
  4. let ItType = document.forms["formtest"]["ItemType"].value;
  5. let ItCon = document.forms["formtest"]["ItemCondition"].value;
  6. let ItDon = document.forms["formtest"]["Donation"].value;
  7. let ConFN = document.forms["formtest"]["FirstName"].value;
  8. let ConLN = document.forms["formtest"]["LastName"].value;
  9. let ConOrg = document.forms["formtest"]["Org"].value;
  10. let Room = document.forms["formtest"]["Room#"].value;
  11. let Shelf = document.forms["formtest"]["Shelf#"].value;
  12. let Row = document.forms["formtest"]["Row#"].value;
  13. let Acerror = document.getElementById("Acerror");
  14. let ItNameerror = document.getElementById("ItemNameerror");
  15. let dateerror = document.getElementById("date");
  16. let ItTypeerror = document.getElementById("ItemTypeerror");
  17. let ItConerror = document.getElementById("ItemConditionerror");
  18. let ItDonerror = document.getElementById("Donationerror");
  19. let Roomerror = document.getElementById("Roomerror");
  20. let Shelferror = document.getElementById("Shelferror");
  21. let Rowerror = document.getElementById("Rowerror");
  22. let ConFNerror = document.getElementById("FirstNameerror");
  23. let ConLNerror = document.getElementById("LastNameerror");
  24. let ConOrgerror = document.getElementById("OrgNameerror");
  25. alert(Ac + ItName + date + ItCon + ItType + Room + Shelf + Row + ItDon);
  26. const Errorstyles = [Acerror, ItNameerror, dateerror, ItTypeerror, ItConerror, Roomerror, Shelferror, Rowerror, ItDonerror];
  27. const blanktext = [Ac, ItName, date, ItCon, ItType, Room, Shelf, Row, ItDon];
  28. alert(blanktext);
  29. blanktext.forEach((i) => {
  30. Errorstyles.forEach((Style) => {
  31. if (i == "") {
  32. Style.style.borderColor = "red";
  33. } else {
  34. Style.style.borderColor = "green";
  35. }
  36. });
  37. })

这样的样式是有效的,只是值不对。谢谢!

英文:

I am getting values from a form and am running a for each loop to see if they are empty. if they are I make the textbox with missing information red and green if not. However, when I put my values throw for each loop. it gets undefined values. I have checked the values in the array after filling the form and they are there. Anyone knows why this happens.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. let Ac = document.forms[&quot;formtest&quot;][&quot;Accession&quot;].value;
  2. let ItName = document.forms[&quot;formtest&quot;][&quot;ItemName&quot;].value;
  3. let date = document.forms[&quot;formtest&quot;][&quot;date&quot;].value;
  4. let ItType = document.forms[&quot;formtest&quot;][&quot;ItemType&quot;].value;
  5. let ItCon = document.forms[&quot;formtest&quot;][&quot;ItemCondition&quot;].value;
  6. let ItDon = document.forms[&quot;formtest&quot;][&quot;Donation&quot;].value;
  7. let ConFN = document.forms[&quot;formtest&quot;][&quot;FirstName&quot;].value;
  8. let ConLN = document.forms[&quot;formtest&quot;][&quot;LastName&quot;].value;
  9. let ConOrg = document.forms[&quot;formtest&quot;][&quot;Org&quot;].value;
  10. let Room = document.forms[&quot;formtest&quot;][&quot;Room#&quot;].value;
  11. let Shelf = document.forms[&quot;formtest&quot;][&quot;Shelf#&quot;].value;
  12. let Row = document.forms[&quot;formtest&quot;][&quot;Row#&quot;].value;
  13. let Acerror = document.getElementById(&quot;Acerror&quot;);
  14. let ItNameerror = document.getElementById(&quot;ItemNameerror&quot;);
  15. let dateerror = document.getElementById(&quot;date&quot;);
  16. let ItTypeerror = document.getElementById(&quot;ItemTypeerror&quot;);
  17. let ItConerror = document.getElementById(&quot;ItemConditionerror&quot;);
  18. let ItDonerror = document.getElementById(&quot;Donationerror&quot;);
  19. let Roomerror = document.getElementById(&quot;Roomerror&quot;);
  20. let Shelferror = document.getElementById(&quot;Shelferror&quot;);
  21. let Rowerror = document.getElementById(&quot;Rowerror&quot;);
  22. let ConFNerror = document.getElementById(&quot;FirstNameerror&quot;);
  23. let ConLNerror = document.getElementById(&quot;LastNameerror&quot;);
  24. let ConOrgerror = document.getElementById(&quot;OrgNameerror&quot;);
  25. alert(Ac + ItName + date + ItCon + ItType + Room + Shelf + Row + ItDon);
  26. const Errorstyles = [Acerror, ItNameerror, dateerror, ItTypeerror, ItConerror, Roomerror, Shelferror, Rowerror, ItDonerror];
  27. const blanktext = [Ac, ItName, date, ItCon, ItType, Room,
  28. Shelf, Row, ItDon
  29. ];
  30. alert(blanktext);
  31. blanktext.forEach((i) =&gt; {
  32. Errorstyles.forEach((Style) =&gt; {
  33. if (i == &quot;&quot;) {
  34. Style.style.borderColor = &quot;red&quot;;
  35. } else {
  36. Style.style.borderColor = &quot;green&quot;;
  37. }
  38. });
  39. })

<!-- end snippet -->

The style works its just the values that don't.
Thanks!

答案1

得分: 1

根据文本的索引选择,而不是遍历所有元素。

  1. const errorElems = [Acerror, ItNameerror, dateerror, ItTypeerror, ItConerror, Roomerror, Shelferror, Rowerror, ItDonerror];
  2. const textValues = [Ac, ItName, date, ItCon, ItType, Room, Shelf, Row, ItDon];
  3. textValues.forEach((text, index) => {
  4. // errorElems[index].classList.toggle('error', text.length === 0);
  5. const color = text.length === 0 ? 'red' : 'green';
  6. errorElems[index].style.borderColor = color;
  7. });
英文:

You need to select based on the index of the text, not loop over all of the elements.

  1. const errorElems = [Acerror, ItNameerror, dateerror, ItTypeerror, ItConerror, Roomerror, Shelferror, Rowerror, ItDonerror];
  2. const textValues = [Ac, ItName, date, ItCon, ItType, Room, Shelf, Row, ItDon];
  3. textValues.forEach((text, index) =&gt; {
  4. // errorElems[index].classList.toggle(&#39;error&#39;, text.length === 0);
  5. const color = text.length === 0 ? &#39;red&#39; : &#39;green&#39;;
  6. errorElems[index].style.borderColor = color;
  7. });

huangapple
  • 本文由 发表于 2023年6月29日 00:29:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定