【问题标题】:jquery/javascript: add to array in case tr has certain classjquery/javascript:添加到数组以防 tr 具有某些类
【发布时间】:2014-09-17 04:30:47
【问题描述】:

我有一个动态 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 中的类是“新”时在 $_REQUESTs 中的值

我该怎么做?

谢谢!

【问题讨论】:

  • 你为什么不像大多数人那样序列化表单并在服务器端计算出来?
  • 只针对.new内的.names/.cities$('.new .names')...
  • billyonecan,我怎样才能接受你的回答?解决了,谢谢!!

标签: javascript jquery arrays class push


【解决方案1】:

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

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

.. 为.cities做同样的事情

【讨论】:

    【解决方案2】:

    你可以试试

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

    【讨论】:

      猜你喜欢
      • 2015-12-14
      • 2018-05-01
      • 2011-01-01
      • 2023-03-26
      • 1970-01-01
      • 2023-03-08
      • 2011-06-04
      • 2013-03-29
      • 1970-01-01
      相关资源
      最近更新 更多