我想使用Jquery在下表中找到II,III,I,D,F和CP的数量。首先,我在考虑使用正则表达式,但对我而言不起作用。我只是一个初学者,这是到目前为止我尝试过的。
桌子:
<table>
<tr>
<th>Sl</th>
<th>Name</th>
<th>status</th>
<th>position</th>
</tr>
<?php while($row= $dbh->fetch(PDO::FETCH_ASSOC)){
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['nm'];?></td>
<td class="st"><?php echo $row['st'];?></td>//Regular AND private
<td class="ps"><?php echo $row['ps'];?></td>//output-CP, I, II, III, D, F
</tr>
<?php } ?>
</table>
jQuery:
var ps = $('.ps').text();//output is IIIIIIDIIIICPIIIIIIDIICPIIFIIII
var compt = ps.match(/CP+/g);
$(".inbody").text(compt.length);//This give me the number of CP i.e. 2
我想要(IIIIIIDIIIICPIIIIIIDIICPIIFIIII)
这种格式的ps值:
II, I,III,D,III,I,CP,III,I,I,I,D,II,CP,II,F,I,III
这样我就可以像下面分别计数(或请提出更好的建议):
var arr = ps.split(",");
var cp = $.inArray("CP",arr).length; //will output 2
var Ist = $.inArray("I",arr).length;//will output 6
var Snd = $.inArray("II",arr).length;//will output 3
var Trd = $.inArray("III",arr).length;//will output 4
var Dnt = $.inArray("D",arr).length;//will output 2
var Fld = $.inArray("F",arr).length;//will output 1
所以我的问题是:如何II, I,III,D,III,I,CP,III,I,I,I,D,II,CP,II,F,I,III
使用jquery从我的表中获取像这样的格式化数据。我也想分别查找处于常规和私有状态的CP职位数量。请帮我。
您可以使用jQuery.map
:
var texts = $.map($('.ps'), function(el) { return $(el).text(); });
console.log(texts);
输出: ["II", "I", "III", "D", "III", "CP"]
因此,如果您希望基于上.st
一行具有数组,则可以执行以下操作:
var texts_private = $.map($('td.st:contains("Private") + td'), function (el) {
return $(el).text();
});
var texts_regular = $.map($('td.st:contains("Regular") + td'), function (el) {
return $(el).text();
});
console.log(texts_private, texts_regular);
它将抓取该.st
行,查看其中包含什么文本,:contains()
然后查看下一行。
该功能将为您提供两个数组:
["I", "D", "CP"] ["II", "III", "III"]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句