如何增加值在单击事件数组 [英] How to add value to an array on click event
本文介绍了如何增加值在单击事件数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想实现是要阅读click事件的价值,并将其保存到一个数组。我的code:
What I am trying to achieve is to read value on click event and save it to an array. My code:
<a href='#' class="hotel" data-hotel-name='Holiday Inn'>Add to favourites</a>
var hotelName = [];
$('.hotel').on('click', function(e){
e.preventDefault();
hotelName.push( $(this).data('hotel-name') );
});
console.log(hotelName.length);
输出始终 0
。有人能告诉我什么我做错了吗?
The output is always 0
. Could someone tell me what am I doing wrong, please?
推荐答案
您的逻辑是好的,你只需要阅读长度
的数组的之后的修改它在点击
处理程序:
Your logic is fine, you just need to read the length
of the array after you modify it in the click
handler:
var hotelName = [];
$('.hotel').on('click', function(e){
e.preventDefault();
hotelName.push( $(this).data('hotel-name') );
console.log(hotelName.length); // < read the length of the amended array here
});
这篇关于如何增加值在单击事件数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文