jQuery使用元素名称创建数组显示多次 [英] jQuery create an array using element name is showing multiple times

查看:82
本文介绍了jQuery使用元素名称创建数组显示多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码使用元素名称创建一个数组,但是它并没有显示一次,而是显示了4次该数组.我现在在做什么错?

I have this code creating an array using the element names, but instead of showing just once it shows the array 4 times. What am I doing wrong now?

HTML

<ul>
    <li><a draggable="true" href="#" id="one" name="uno" class="imgHvr">One</a></li>
    <li><a draggable="true" href="#" id="two" name="dos" class="imgHvr">2</a></li>
    <li><a draggable="true" href="#" id="three" name="tres" class="imgHvr">three</a></li>
    <li><a draggable="true" href="#" id="four" name="sweet" class="imgHvr">4</a></li>
</ul>

jQuery

$("#subBtn").click(function() {
    var answers = []
    $('#bin a').each(function() {
        answers.push($(this).attr('name'));
        console.log(answers);
    })
});

我已在此处演示

推荐答案

以答复形式:

$("#subBtn").click(function() {
    var answers = []
    $('#bin a').each(function() {
        answers.push($(this).attr('name'));
    })
    console.log(answers);
});

这篇关于jQuery使用元素名称创建数组显示多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆