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 badges4 Answers
Reset to default 3You 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
版权声明:本文标题:javascript - on each button click store value in array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741974112a2408018.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论