2014-11-21 5 views
1

배열에 클릭 된 버튼의 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")'에 대한 몇 가지 유용한 링크가; –

+0

클릭 한 버튼 값을 하나의 배열에 저장하는 방법 $ (document) .on ('click', '.btn', function() { alert ($ (this) .val)); VAR myvalues ​​= $ (이) .val(); 경고 (myvalues) VAR 차 = [myvalues, "myvaluesv", "도요타"]; 경고 (자동차) var에 엑스 = cars.length; alert (x); }); –

+0

정말 확실합니까? 코드에 오류가 있습니까? –

답변

2

당신이 시도 할 수이

 <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

+0

작동하지만 이제 정적 값을 가진 최종 배열을 비교하는 방법 –

+0

예 할 수 있습니다 ... 최종 배열은 전역이므로 어디서든 스크립트를 작성할 수 있습니다 .btw 더 구체적으로 필요한 것을 말할 수 있습니까? 그럼 내가 도울 수 있습니다. –

+0

하나 이상의 배열을 선언하고 새 배열과 동일한 버튼을 클릭하여 배열에 입력 한 값을 다음 페이지로 이동하려면 var arrt = new Array; \t arrt = [ "cat", "lion", "dog", 1, 2, 1, 1, 2, 3]; 그리고 우리는 –

0

사용 Array.prototype.push()있어서, 상기 어레이의 끝에서 새로운 값을 추가하는 방법 : 여기

코드이다. 또한 새로운 길이를 반환합니다.

cars.push(myvalues, "myvaluesv", "Toyota") 예를 들어 모든 데이터 유형을 배열로 푸시 할 수 있습니다.

관련 문제