【问题标题】:how to add values one by one in an array [duplicate]如何在数组中一一添加值[重复]
【发布时间】:2015-01-20 04:05:22
【问题描述】:

我想在一个数组中添加 8 个被点击按钮的值。

当用户按下任何按钮时,该按钮的 id/值存储在数组中,但我的数组仅在数组 [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>

【问题讨论】:

  • arr.push("new_value");
  • 但是如何将每个单击的按钮值保存在一个数组中?当我通过 $(document).on('click', '.btn', function(){ alert($( this).val()); var myvalues=$(this).val(); alert(myvalues); var cars =[myvalues, "myvaluesv", "Toyota"]; alert(cars); var x = 汽车。长度;警报(x);});
  • 你确定吗?你的代码没有错误?

标签: javascript arrays


【解决方案1】:

你可以试试这个

     <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

【讨论】:

  • 它可以工作,但现在如何将最终数组与静态值进行比较
  • 是的,你可以......你的最终数组是全局的,所以你可以在我们的脚本中找到任何地方。顺便说一句,你能说更具体的你需要......然后我可以帮助你
  • 我想再声明一个数组,如果通过单击按钮在数组中输入的值等于新数组,则转到下一页: var arrt=new Array; arrt = [“猫”、“狮子”、“狗”、1、2、1、1、2、3];我们
  • 你想比较两个数组吗?
  • 是的,如果两者相同,我想比较 arr 数组和 arr 数组,然后提醒或移至下一页
【解决方案2】:

使用Array.prototype.push() 方法在数组末尾添加一个新值。它也会返回新的长度。

cars.push(myvalues, "myvaluesv", "Toyota") 为您的示例。您可以将任何数据类型推送到数组中。

【讨论】:

    猜你喜欢
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 2010-12-30
    • 1970-01-01
    相关资源
    最近更新 更多