<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">
在每个按钮上单击,将单击的值存储在数组中 我已经尝试过了
$( document ).ready(function() {
$('.sal').each(function() {
$(this).click(function(i) {
var i[]=$(this).val();
console.log(i);
});
});
});
我在这里缺少什么!!!!!!
最佳答案
您可以在您的 html 中尝试此代码:
var i = [];
$( document ).ready(function() {
$('.sal').each(function() {
$(this).click(function(e) {
i.push($(this).val());
console.log(i);
});
});
});
你的代码有什么问题吗?
首先,您没有正确初始化 i
数组。您还需要将其定义在单击功能的范围之外,因为在功能完成后您将失去它。如果您不想污染全局范围,您可以在 ready()
方法的函数内定义数组。
关于javascript - 在每个按钮上单击将值存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37296196/