使用 Jquery 获取多个元素的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9057388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:16:13  来源:igfitidea点击:

Getting multiple elements' value with Jquery

jqueryarrays

提问by FlyingCat

I am trying to get the value of a table cell.

我正在尝试获取表格单元格的值。

For example

例如

<table id='projects'>

        <tr>
        <td id='a'>aaaaa</td>
        <td id='b'>bbbbb</td>
        <td id='c'>ccccc</td>
        <td id='d'>eeeee</td>
        <td id='e'>ddddd</td>
        </tr>
</table>
<a id='test' href='#'>test </a>

I want to get aaaaa,bbbbb,ccccc,eeeee,ddddd and asign them to my array

我想得到 aaaaa、bbbbb、ccccc、eeeeee、ddddd 并将它们分配给我的数组

I believe I can get the value with my Jquery code below

我相信我可以通过下面的 Jquery 代码获得价值

$(document).ready(function(){
    $('#test').click(function(){
    var tableVal=new Array();
    tableVal['a']=$('#a').text();
    tableVal['b']=$('#b').text();
    tableVal['c']=$('#c').text();
    tableVal['d']=$('#d').text();
    tableVal['e']=$('#e').text();   

    })
 });

However, I think it's not very maintainable and take lots of code if I have 20 tags. I was wondering if I can do it with .eachor any better way to archive this. Thanks for the help.

但是,如果我有 20 个标签,我认为它不是很容易维护并且需要大量代码。我想知道我是否可以用.each或任何更好的方式来存档它。谢谢您的帮助。

采纳答案by Selvakumar Arumugam

You can do something like below,

您可以执行以下操作,

var tableVal= [];

$('#projects tr:eq(0) td').each (function () {
   tableVal[this.id] = $(this).text();
});

Note: :eq(0)- means 1st row.. Modify accordingly if you want to do for all rows or let me know if you need help with that.

注意::eq(0)- 表示第一行。如果你想对所有行做相应的修改,或者如果你需要帮助,请告诉我。

回答by

If you actually want an Array, use .map()with .toArray().

如果您确实想要一个数组,请使用.map()with .toArray()

var tableVal = $('#projects td').map(function(i,v) {
    return $(this).text();
}).toArray();

Otherwise if you're actually going to use non numeric indices, you want an Object, using the techniques in the other answers.

否则,如果您实际上要使用非数字索引,则需要一个对象,使用其他答案中的技术。

回答by clime

Yes, you can :).

是的你可以 :)。

var tableVals= {}
$('#projects td').each(function() {
    tableVals[$(this).attr('id')] = $(this).text();
});

Please, remember to use object instead of array if your keys are not numeric.

如果您的键不是数字,请记住使用对象而不是数组。

回答by Alec Gorge

First, give your tran id:

首先,给你tr一个id

<tr id="rowToGetDataFrom">

Then you can get the array you want like this:

然后你可以像这样得到你想要的数组:

var tableVal = $('#rowToGetDataFrom td').map(function () { return $(this).text(); });

Demo: http://jsfiddle.net/alecgorge/3ApnB/

演示:http: //jsfiddle.net/alecgorge/3ApnB/

回答by David says reinstate Monica

Try:

尝试:

var tableVal = [];
$('#test').click(
    function(){
        $('#projects td').each(
             function(){
                  tableVal.push($(this).text());
             });
    });