Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将一个输入框中的值添加到数组中?如果我在输入字段中输入数字,我如何得到这些数字的总和?_Javascript_Jquery - Fatal编程技术网

Javascript 如何将一个输入框中的值添加到数组中?如果我在输入字段中输入数字,我如何得到这些数字的总和?

Javascript 如何将一个输入框中的值添加到数组中?如果我在输入字段中输入数字,我如何得到这些数字的总和?,javascript,jquery,Javascript,Jquery,如何将输入值从一个输入字段存储到一个数组。如果我输入数字,那么我怎么能得到这些数字的总和呢?这是我的密码。 我真的很感谢你们的帮助 我的css <style> .item-check { display: inline-block; position: absolute; width: 22px; height: 21px; curs

如何将输入值从一个输入字段存储到一个数组。如果我输入数字,那么我怎么能得到这些数字的总和呢?这是我的密码。 我真的很感谢你们的帮助

我的css

        <style>
        .item-check {
            display: inline-block;
            position: absolute;
            width: 22px;
            height: 21px;
            cursor: pointer;
          background-image: url('https://cdn.glitch.com/72548e86-2a07-45d1-9756-a034ea6672b3%2Fincomplete.png?1495684448133');
        }

        .complete {
            background-image: url('https://cdn.glitch.com/72548e86-2a07-45d1-9756-a034ea6672b3%2Fcomplete.png?1495684439106');
        }

        .item-text {
            margin-left: 2em;
            width: 22px;
            height: 21px;
        }

        .item-remove {
          float: right;
            cursor: pointer;
          width:20px;
          height:20px;
          background-size:cover;
          background-image: url('https://cdn.glitch.com/72548e86-2a07-45d1-9756-`a034ea6672b3%2Fremove.png?1495684443965');`
        }
        </style>

.项目检查{
显示:内联块;
位置:绝对位置;
宽度:22px;
高度:21px;
光标:指针;
背景图像:url('https://cdn.glitch.com/72548e86-2a07-45d1-9756-a034ea6672b3%2Fincomplete.png?1495684448133');
}
.完成{
背景图像:url('https://cdn.glitch.com/72548e86-2a07-45d1-9756-a034ea6672b3%2Fcomplete.png?1495684439106');
}
.项目案文{
左边距:2米;
宽度:22px;
高度:21px;
}
.项目移除{
浮动:对;
光标:指针;
宽度:20px;
高度:20px;
背景尺寸:封面;
背景图像:url('https://cdn.glitch.com/72548e86-2a07-45d1-9756-`a034ea6672b3%2删除.png?14956843965’`
}
我的HTML

   <html>
      <head>
        <meta charset="UTF-8">
        <title>My To-do List</title>
        <script src="https://code.jquery.com/jquery.min.js"></script>
        <link rel="stylesheet" href="/style.css" type="text/css">
      </head>  
  <body>
    <div class="container">
      <header>
        <h1>My To-do List</h1>
      </header>

      <!--   Key Section of App    -->
      <form class="list-content">

        <label>
          <input type="text" class="item-input">
        </label>
        <h4 id="message"></h4>
        <ul class="shopping-list">
          <!-- <li>Sample Item</li> -->
        </ul>

        <button class="add-item">Add    Item</button>
      </form>
      <!--   Key Section of App    -->

    </div>
   </body>
  </html>

我的任务清单
我的任务清单
添加项
我的Javascript代码

//第一步 //此代码在有人单击“添加项目”按钮时执行 //在购物项目的右上角 //-----------------

   <script>
    $(function() {
      $(".add-item").on('click', function(event) {
          if ($('.item-input').val() == "") {
              alert ('Please enter some text!');
              return false;
          } else {
              var listItem = $(".item-input").val();
              var itemHtml = "<li><span class='item-check'></span><span class='item-text'>" + listItem + "</span><span class='item-remove'></span></li>";
              $(".shopping-list").append(itemHtml);

            //Remove the text the user entered from item-input   
              $('.item-input').val();
              $('.item-input').val('');

            //Count items entered
            var addedItem = $('.item-text').length;
            document.getElementById("message").innerHTML=("You have entered " + addedItem + " items in your list");

              event.preventDefault();
          };    
      });         

    // -------------------

    // This code is executed when someone clicks the "X" button
        $(".shopping-list").on('click', '.item-remove', function(event) {
        $(event.currentTarget).parent().remove();

        });

    // -------------------

    // This code is executed when someone clicks the checkbox in the shopping-item section
        $(".shopping-list").on('click', '.item-check', function(event) {
        $(event.currentTarget).toggleClass('complete');

      });

    });
    </script>

$(函数(){
$(“.add item”)。在('click',函数(事件){
如果($('.item input').val()==“”){
警报('请输入一些文本!');
返回false;
}否则{
var listItem=$(“.item input”).val();
var itemHtml=“
  • ”+listItem+”
  • ”; $(“.shopping list”).append(itemHtml); //从项目输入中删除用户输入的文本 $('.item input').val(); $('.item input').val(''); //清点输入的项目 var addedItem=$('.item text')。长度; document.getElementById(“message”).innerHTML=(“您已经在列表中输入了“+addedItem+”项”); event.preventDefault(); }; }); // ------------------- //此代码在有人单击“X”按钮时执行 $(“.shopping list”)。在('单击','项目删除',函数(事件){ $(event.currentTarget).parent().remove(); }); // ------------------- //当有人单击购物项目部分中的复选框时,将执行此代码 $(“.shopping list”)。在('点击','项目检查',功能(事件){ $(event.currentTarget).toggleClass('complete'); }); });
    研究JavaScript
    push()
    方法。然后研究如何使用
    for
    循环遍历数组中的值。您可以提供输出测试用例吗?上面的链接是您可以测试我的应用程序的地方。谢谢你的回答。@Nirali你能在这里测试一下吗