jquery / javascript:如果tr有某个类,则添加到数组

时间:2022-09-03 21:44:17

I have a dynamic html table:

我有一个动态的html表:

<table>
<tbody>
<tr>
<td>Name</td>
<td>City</td>
</tr> 
<tr class="new" id="item1">
<td><input class="names" value="John Smith" type="hidden">John Smith</td>
<td><input class="cities" value="London" type="hidden">London</td>
</tr>
<tr class="normal" id="item2">
<td><input class="names" value="Regina Mills" type="hidden">Regina Mills</td>
<td><input class="cities" value="Berlin" type="hidden">Berlin</td>
</tr>
<tr class="normal" id="item3">
<td><input class="names" value="Marcus Bell" type="hidden">Marcus Bell</td>
<td><input class="cities" value="Liverpool" type="hidden">Liverpool</td>
</tr>
</tr>
</tbody>
</table>

From my js file I'm storing the information in this way:

从我的js文件中我以这种方式存储信息:

     var arrayNames = [];
        $(".names").each(function(){
            arrayNames.push($(this).val());
        })

         var arrayCities = [];
        $(".cities").each(function(){
            arrayCities.push($(this).val());
        })

 params += '&names='+arrayNames;
 params += '&cities='+arrayCities;

With this I am getting in my php:

有了这个我进入我的PHP:

$_REQUEST['names']
: string = John Smith,Regina Mills,Marcus Bell
$_REQUEST['cities']
: string = London,Berlin,Liverpool

But I only need in the $_REQUESTs the values when the class in the table tr's is "new"

但是当表tr中的类是“new”时,我只需要$ _REQUESTs中的值

How can I do that?

我怎样才能做到这一点?

Thanks!

谢谢!

2 个解决方案

#1


1  

You could try

你可以试试

$(".names").each(function(){
        if ($( this ).parent().attr( "class" ) == "new")
            arrayNames.push($(this).val());
        })

#2


0  

Just target the .names within .new. You can use map() to get the values:

只需将.names中的.names定位。您可以使用map()来获取值:

var arrayNames = $('.new .names').map(function() { return this.value; }).get();

.. and do the same thing for .cities

..并为.cities做同样的事情

#1


1  

You could try

你可以试试

$(".names").each(function(){
        if ($( this ).parent().attr( "class" ) == "new")
            arrayNames.push($(this).val());
        })

#2


0  

Just target the .names within .new. You can use map() to get the values:

只需将.names中的.names定位。您可以使用map()来获取值:

var arrayNames = $('.new .names').map(function() { return this.value; }).get();

.. and do the same thing for .cities

..并为.cities做同样的事情