我想找到II,III,I,d,F和CP下表中使用jQuery的數量。首先,我正在考慮使用正則表達式,但它不適用於我。我只是一個初學者,這是我迄今爲止所嘗試的。查找和計數字符串使用jQuery
表:
<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
我想在這個格式的PS值(IIIIIIDIIIICPIIIIIIDIICPIIFIIII)
:
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
所以我的問題是:我怎麼能從我的表使用jQuery獲得像這樣的格式化數據II, I,III,D,III,I,CP,III,I,I,I,D,II,CP,II,F,I,III
。我想分別在常規和私人狀態中查找CP位置的數量。請幫幫我。
停止,使用PDO /庫MySQLi代替。而使用PHP短標籤可能會導致兼容性問題。儘量避免他們 – Raptor
@Raptor,是的,請。我知道。但這不會幫助我,因爲我的問題只是jQuery,而不是PHP的一部分。 –
@LalhriatpuiiMapuii發佈生成的HTML而不是服務器端代碼,因爲這個問題是關於jQuery/JS的。 – lshettyl