【问题标题】:adding statement to increment value by 1 js添加语句以将值增加 1 js
【发布时间】:2021-11-23 12:53:33
【问题描述】:

嗨,我对 javascript 真的很陌生,并且坚持一个教科书练习,我将变量 i 的增量增加 1,以便能够继续列出输入。到目前为止,这是我的代码,任何人都可以指导我做错了什么,谢谢

<body>
   <header>
      <h1>
         Project 2 Test 1
      </h1>
   </header>

   <article>
      <div id="results">
          <ul>
             <li id="item1"></li>
             <li id="item2"></li>
             <li id="item3"></li>
             <li id="item4"></li>
             <li id="item5"></li>
          </ul>
          <p id="resultsExp"></p>
      </div>
      <form>
          <fieldset>
            <label for="wishList" id="placeLabel">
              Type the name of a wish list item, then click Submit:
            </label>
            <input type="text" id="wishList" />
          </fieldset>
          <fieldset>
            <button type="button" id="button">Submit</button>
          </fieldset>
      </form>
   </article>
   <script>
      i = 1;
      listitem = "";
      function processitems() {
         if (i<= 5) {
            listitem ="item" +i;
            document.getElementById(listitem).innerHTML = document.getElementById("wishList").value;
            document.getElementById("wishlist").value ="";
            if (i===5) {
               document.getElementById("resultsExp").innerHTML = "Your wish list has been submitted";
            }
//inside the processitems() function of the main if statement, after the nested if statement, add a statement to increment the value of i by 1.
          i =+1;
         }
      }
      var btn = document.getElementById("button");
      if (btn.addEventListener) {
         btn.addEventListener("click", processitems, false);
      } else if (btn.attachEvent) {
         btn.attachEvent("onclick", processitems);
      }
   </script>

【问题讨论】:

  • 你错过了最后一个:++i

标签: javascript variables increment


【解决方案1】:

i =+1;1 分配给i。你应该改用i += 1

<body>
  <header>
    <h1>
      Project 2 Test 1
    </h1>
  </header>

  <article>
    <div id="results">
      <ul>
        <li id="item1"></li>
        <li id="item2"></li>
        <li id="item3"></li>
        <li id="item4"></li>
        <li id="item5"></li>
      </ul>
      <p id="resultsExp"></p>
    </div>
    <form>
      <fieldset>
        <label for="wishList" id="placeLabel">
              Type the name of a wish list item, then click Submit:
            </label>
        <input type="text" id="wishList" />
      </fieldset>
      <fieldset>
        <button type="button" id="button">Submit</button>
      </fieldset>
    </form>
  </article>
  <script>
    i = 1;
    listitem = "";

    function processitems() {
      if (i <= 5) {
        listitem = "item" + i;
        document.getElementById(listitem).innerHTML = document.getElementById("wishList").value;
        document.getElementById("wishList").value = "";
        if (i === 5) {
          document.getElementById("resultsExp").innerHTML = "Your wish list has been submitted";
        }
        //inside the processitems() function of the main if statement, after the nested if statement, add a statement to increment the value of i by 1.
        i += 1;
      }
    }
    var btn = document.getElementById("button");
    if (btn.addEventListener) {
      btn.addEventListener("click", processitems, false);
    } else if (btn.attachEvent) {
      btn.attachEvent("onclick", processitems);
    }
  </script>

【讨论】:

  • 嘿,伙计,我刚刚看到我用小写而不是大写的“wishList”解决了我的问题,谢谢你的帮助!
  • @Panoply 没问题 :) 如果它解决了你的问题,请考虑accepting我的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
  • 2012-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多