admin管理员组

文章数量:1315017

<input type="button" class="sal" value="0">
<input type="button" class="sal" value="0">
<input type="button" class="sal" value="0">
<input type="button" class="sal" value="0">

On each button click store clicked value in a array I have tried this

$( document ).ready(function() {
            $('.sal').each(function() {
            $(this).click(function(i) {                    
                 var i[]=$(this).val();
                console.log(i);
            }); 
          });
});

What I am missing here !!!!!

<input type="button" class="sal" value="0">
<input type="button" class="sal" value="0">
<input type="button" class="sal" value="0">
<input type="button" class="sal" value="0">

On each button click store clicked value in a array I have tried this

$( document ).ready(function() {
            $('.sal').each(function() {
            $(this).click(function(i) {                    
                 var i[]=$(this).val();
                console.log(i);
            }); 
          });
});

What I am missing here !!!!!

Share Improve this question edited May 18, 2016 at 11:35 Jack jdeoel 4,5846 gold badges28 silver badges56 bronze badges asked May 18, 2016 at 9:57 arpit Pathakarpit Pathak 471 gold badge2 silver badges10 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

You can try this code with your html:

var i = [];

    $( document ).ready(function() {        
        $('.sal').each(function() {
                $(this).click(function(e) {        
                     i.push($(this).val());
                    console.log(i);
                }); 
         });
    });

JSFiddle

What is wrong with your code? first of all you are not initializing the i array properly. also you need to define it outside of the scope of the click function because you are going to loose it after the function is done. If you do not want to pollute the global scope you can define the array inside the function of the ready() method.

Try this

$( document ).ready(function() {
    var arr=new Array();
    $('.sal').each(function() {
        $(this).click(function(i) {
            arr.push($(this).val());
        }); //missing ); here!
    });
});

You can create array to store values of all clicked buttons:

Fiddle: https://jsfiddle/1tbLbmkg/

    var values = []; // declare it outside document.ready
    $( document ).ready(function() {   
        $('.sal').click(function(i) {
             values.push($(this).val()); // add value to array
        }); 
    }); 

You can simplifyy this function. note that since all inputs contain 0 as their value- you will end up with a bunch of zeroes in the aray. You also have to declare the empty array before pushing the value into it. Fiddle:https://jsfiddle/gavgrif/xL7qsbsn/

$( document ).ready(function() {
            var newArray= new Array();
            $('.sal').click(function() {
                 newArray.push($(this).val());
                  alert("newArray contents = "+ newArray)
            });
       });
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" class="sal" value="0">
<input type="button" class="sal" value="0">
<input type="button" class="sal" value="0">
<input type="button" class="sal" value="0">

本文标签: javascripton each button click store value in arrayStack Overflow