javascript - jquery/javascript : add to array in case tr has certain class

标签 javascript jquery arrays class push

我有一个动态 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>

从我的 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;

这样我就可以进入我的 php 了:

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

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

我怎样才能做到这一点?

谢谢!

最佳答案

你可以试试

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

关于javascript - jquery/javascript : add to array in case tr has certain class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24953694/

相关文章:

javascript - 如何在 d3 图表中定位图例

jquery绑定(bind)按钮点击事件失败

javascript - 使用多个选择选项过滤数组

arrays - Kadane 的算法是否适用于运行长度编码的整数数组?

javascript - 使用适配器和序列化器通过版本 2 WP REST API 将 Ember 2.3 应用程序连接到 WordPress 后端

javascript - 为什么这个简单的 JQuery 代码会抛出错误?

javascript - 如何从 R Shiny 中的 JavaScript 获取屏幕分辨率?

jquery - 使用jquery按需禁用一系列单选按钮

javascript - 当标题的大小(宽度/高度)调整时,元素填充会调整

python - 迭代时如何获取 2D Numpy 数组中的位置?