猿问

为什么 JavaScript 没有进入 For...in 循环

出于某种原因,当我逐行执行时,下面的 for...in 循环不会执行。看起来它读取 Object 和 'let outerLoopProp' 语句,然后跳过整个循环体。


循环位于表单提交事件中。


我已将对象记录到控制台,并在代码到达 for..in 循环时用属性填充它。


form1.addEventListener("submit", (e) => {

  e.preventDefault();


  let forValidationCheck = {};

  let allNames = ["Tarp 1", "Tarp 2", "Tarp 3"];


  for(let r = 0; r < allNames.length; r++) {

                    

    let currentChosen = allNames[r].toLowerCase();

    let optionsForCheck = document.getElementById(`selection${r+1}`);

    let selectedArrayForCheck = [];

                    

    for(z=0; z < optionsForCheck.options.length; z++) {

      if(optionsForCheck.options[z].selected === true) {

          selectedArrayForCheck.push(optionsForCheck.options[z].value.toLowerCase());

      }

    }

    Object.defineProperty(forValidationCheck, currentChosen, {

        value: selectedArrayForCheck

    })

  }


  for(let layerOuterLoop in forValidationCheck) {

      //Code within for..in loop

  }

});

<form id="form1">


  <select name="selection1" id="selection1">

    <option value="Tarp 1">Tarp 1</option>

    <option value="Tarp 2">Tarp 2</option>

  </select>


  <select name="selection2" id="selection2" >

    <option value="Tarp 2">Tarp 2</option>

    <option value="Tarp 3">Tarp 3</option>

  </select>


  <select name="selection3" id="selection3">

    <option value="Tarp 1">Tarp 1</option>

    <option value="Tarp 3">Tarp 3</option>

  </select>


  <button type="submit">submit</button>

</form>


翻过高山走不出你
浏览 297回答 2
2回答

波斯汪

解决方法:在使用Object.defineProperty()时,需要为方法的第三个参数设置enumerable:true。见下文。感谢大家的帮助!form1.addEventListener("submit", (e) => {&nbsp; &nbsp; e.preventDefault();&nbsp;let forValidationCheck = {};&nbsp;let allNames = ["Tarp 1", "Tarp 2", "Tarp 3"];&nbsp; &nbsp; for(let r = 0; r < allNames.length; r++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let currentChosen = allNames[r].toLowerCase();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let optionsForCheck = document.getElementById(`selection${r+1}`);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let selectedArrayForCheck = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(z=0; z < optionsForCheck.options.length; z++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(optionsForCheck.options[z].selected === true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectedArrayForCheck.push(optionsForCheck.options[z].value.toLowerCase());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object.defineProperty(forValidationCheck, currentChosen, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: selectedArrayForCheck,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enumerable: true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(let layerOuterLoop in forValidationCheck) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Code within for..in loop&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }});<html><head></head><body><form id="form1">&nbsp; <select name="selection1" id="selection1">&nbsp; &nbsp; <option value="Tarp 1">Tarp 1</option>&nbsp; &nbsp; <option value="Tarp 2">Tarp 2</option>&nbsp; </select>&nbsp; <select name="selection2" id="selection2">&nbsp; &nbsp; <option value="Tarp 2">Tarp 2</option>&nbsp; &nbsp; <option value="Tarp 3">Tarp 3</option>&nbsp; </select>&nbsp; <select name="selection3" id="selection3">&nbsp; &nbsp; <option value="Tarp 1">Tarp 1</option>&nbsp; &nbsp; <option value="Tarp 3">Tarp 3</option>&nbsp; </select></form></body></html>

LEATH

做同样的事情更简单:const myForm&nbsp; &nbsp;= document.getElementById('form1')&nbsp; &nbsp; , allNames ={ 'Tarp 1': myForm.selection1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , 'Tarp 2': myForm.selection2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , 'Tarp 3': myForm.selection3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }myForm.onsubmit=e=>&nbsp; {&nbsp; e.preventDefault()&nbsp; let forValidationCheck = {}&nbsp; for (let aN in allNames)&nbsp; &nbsp; {&nbsp; &nbsp; forValidationCheck[aN.toLowerCase()] = allNames[aN].value&nbsp; &nbsp; //forValidationCheck[aN.toLowerCase()] = [allNames[aN].value] //if prefered&nbsp;&nbsp; &nbsp; }&nbsp; for (let layerOuterLoop in forValidationCheck)&nbsp; &nbsp; {&nbsp; &nbsp; console.log(layerOuterLoop, ' => ',forValidationCheck[layerOuterLoop] )&nbsp; &nbsp; }&nbsp; }/* -- in case of selects are multiple : -...&nbsp; for (let aN in allNames)&nbsp; &nbsp; {&nbsp; &nbsp; forValidationCheck[aN.toLowerCase()] = [...allNames[aN].options]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(option=>option.selected)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(option=>option.value)&nbsp; &nbsp; }*/<form id="form1">&nbsp; <select name="selection1">&nbsp; &nbsp; <option value="Tarp 1">Tarp 1</option>&nbsp; &nbsp; <option value="Tarp 2">Tarp 2</option>&nbsp; </select>&nbsp; <select name="selection2">&nbsp; &nbsp; <option value="Tarp 2">Tarp 2</option>&nbsp; &nbsp; <option value="Tarp 3">Tarp 3</option>&nbsp; </select>&nbsp; <select name="selection3">&nbsp; &nbsp; <option value="Tarp 1">Tarp 1</option>&nbsp; &nbsp; <option value="Tarp 3">Tarp 3</option>&nbsp; </select>&nbsp; <button type="submit">submit</button></form>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答