按钮把值放入数组 [英] Put value of buttons into array

查看:115
本文介绍了按钮把值放入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有大约在页面上十几HTML按钮,所有分配给他们一个独特的价值属性。

I currently have about a dozen html buttons on a page, all with a unique value attribute assigned to them.

首先,我希望能够得到这些按钮的值,并将其分配到一个数组。这里是我的code:

Firstly, I want to be able to get the values of these buttons and assign them into an array. Here is my code:

            var myArray = [];

            $("#buttonID").each(function(){
                myArray.push($(this).attr("value"));
            });

此工作,然而只需要从第一按钮的值,然后忽略其余部分,尽管它们都具有相同的ID。我做错了我。每次()?

This works, however only takes the value from the first button, and then ignores the rest, despite them all have the same ID. Have I done something wrong with my .each() ?

在我已经解决了,我想,然后修改上面只与他们。主动类添加这些按钮的值。即用户已选择它们。

Once I have solved that, I would like to then modify the above to only add values of those buttons with ".active" classes on them. i.e a user has selected them.

推荐答案

您选择重新presents一个ID因此这究竟是为什么它选择只有一个,因为ID的应该是唯一的,你需要通过类名来接他们像 .className 分配此类名所有按钮后

Your selector represents an ID hence the #this why it picks only one because ID's are supposed to be unique, you need to pick them by class name like .className after assigning this class name to all of your buttons

在这个 http://www.w3schools.com/jquery/jquery_ref_selectors.asp有loonk

这篇关于按钮把值放入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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