如何逐个将值添加到数组中

5

我希望能将点击按钮的8个值添加到一个数组中。

当用户按下任何按钮时,该按钮的id / value存储在数组中,但是我的数组仅在数组[0]中保存第一个值。

如何将其他值插入数组中?

以下是代码:

function reply_click(obj) {
  var id = [obj.id];
  for (i = 0; i <= id; i++) {
    alert(id);
  }
}

var arr = [id];

alert(arr);

if (id[0] == "1") {
  alert(6666666666);
}

if (id[0] == "2") {
  id[0] = 1;
  id[1] = 2;
  alert(777777777777);
}


if (id[0] == "3") {
  id[0] = 1;
  id[1] = 2;
  id[2] = 3;
  alert('login');
}

alert(id);

$(document).on('click', '.btn', function() {

  alert($(this).val());

  var myvalues = $(this).val();
  alert(myvalues);

  var cars = [myvalues, "myvaluesv", "Toyota"];
  alert(cars);
  var x = cars.length;
  alert(x);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="1" onClick="reply_click(this)">1</button>
<button id="2" onClick="reply_click(this)">2</button>
<button id="3" onClick="reply_click(this)">3</button>

<button id="tahira" name="btn" class="btn" value="tahirahassan">hi tahira</button>
<button id="sobia" name="btn" class="btn" value="sobia">hi sobia</button>
<form action="testingjquery.php" method="post">
  <input type="submit" id="btn1" class="btn111" name="val1" value=" i am tahira" />
  <input type="submit" id="btn2" class="btn112" name="val1" value="hassan" />
</form>


1
arr.push("new_value"); - RNK
但是如何将每个点击按钮的值保存在一个数组中呢?当我通过$(document).on('click', '.btn', function(){获取值时, alert($(this).val()); var myvalues=$(this).val(); alert(myvalues);var cars =[myvalues, "myvaluesv", "Toyota"]; alert(cars); var x = cars.length; alert(x); }); - Tahira Hassan
你确定吗?你的代码没有错误吗? - Rajib Ghosh
2个回答

3
你可以试试这个。
     <html>
      <head>
         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
         <script>
          var arr=new Array;
          //unique object variable
          var trackObject = {};
          function reply_click(obj){
              var id = [obj.id];

                //check value is not duplicated 
                 if(!trackObject.hasOwnProperty(id)){
                    trackObject[id] = 1;
                    for(i=1;i<=id;i++){
                      arr.push(i);
                    }
                 }
                console.log(arr);

          } 
          $(document).ready(function(){
              $(".btn").click(function(){
                var myvalues=$(this).val();
                //check value is not duplicated
                 if(!trackObject.hasOwnProperty(myvalues)){
                    trackObject[myvalues] = 1;
                    arr.push(myvalues);

                  }
                  console.log(arr);

              });
          });
          // attach the .equals method to Array's prototype to call it on any array
      Array.prototype.equals = function (array) {
          // if the other array is a falsy value, return
          if (!array)
              return false;

          // compare lengths - can save a lot of time 
          if (this.length != array.length)
              return false;

          for (var i = 0, l=this.length; i < l; i++) {
              // Check if we have nested arrays
              if (this[i] instanceof Array && array[i] instanceof Array) {
                  // recurse into the nested arrays
                  if (!this[i].equals(array[i]))
                      return false;       
              }           
              else if (this[i] != array[i]) { 
                  return false;   
              }           
          }       
          return true;
      }  
      function compare(){

        result=arr.equals([1, 1, 2, 1, 2, 3, "tahirahassan", "sobia"]);
        alert(result);
      }
          </script>
          </head>


          <body >
              <p>Click on this paragraph.</p>
            <button id="1" onClick="reply_click(this)">1</button>
          <button id="2" onClick="reply_click(this)">2</button>
           <button id="3" onClick="reply_click(this)">3</button>

        <button id="tahira" name="btn1" class="btn" value="tahirahassan" >hi tahira</button>
       <button id="sobia" name="btn2" class="btn" value="sobia" >hi sobia</button>
         <form action="testingjquery.php" method="post">
          <input type="submit" id="btn1" class="btn111" name="val1" value=" i am tahira"/>
          <input type="submit" id="btn2" class="btn112" name="val1" value="hassan" />
        </form>
        <button id="compare" onClick="compare()">compare</button>

      </body>

      </html>

以下是一些有用的链接:push-arrayunique-value


是的,你可以...你的最终数组是全局的,所以你可以在脚本的任何地方使用它。顺便问一下,你能说得更具体一些你需要什么...然后我可以帮助你。 - Rajib Ghosh
我想声明一个新的数组,如果通过点击按钮输入的数组值等于新数组,则跳转到下一页: var arrt = new Array; arrt = ["猫", "狮子", "狗", 1, 2, 1, 1, 2, 3]; - Tahira Hassan
add those line with in script - Rajib Ghosh
谢谢,明白了。我还想问一件事,有没有办法在arr数组输入8个条目之后自动比较数组,而不需要按比较按钮? - Tahira Hassan
非常感谢。正在工作中。 - Tahira Hassan
显示剩余11条评论

0
使用Array.prototype.push()方法在数组末尾添加新值。它还会返回新的长度。
对于你的示例,可以使用cars.push(myvalues, "myvaluesv", "Toyota")。你可以将任何数据类型推入数组中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接