如果使用jQuery在表中存在某些单词,我需要增加一个计数器。当我预计它应该增加2时,我的计数器只增加1。
HTML& jQuery如下:
var counter = 0;
$($(".ProductSKULabel:contains('LY-')").length || $(".ProductSKULabel:contains('LYE-')").length || $(".ProductSKULabel:contains('HSE-')").length || $(".ProductSKULabel:contains('HS-')").length).each(function() {
counter++;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="w3_tblCartContents" class="CartTable" cellspacing="0" cellpadding="0" border="0" style="width:100%;border-collapse:collapse;">
<tbody>
<tr class=" CartHeader">
<th></th>
<th class="ProductSKUColumn" colspan="2">Product</th>
<th>Qty</th>
<th>Total</th>
</tr>
<tr class="AlternatingRowStyle">
<td class="ProductSKUColumn" align="left" colspan="2"><a class="ItemNameLabel" href="/Product/Top-Loading-All-Purpose-Vinyl-Badge-Holders---3-x-4---Clip-Attachment---Clear-HSE-6-C-CLR-96101.htm?cartprodid=28011557">Top-Loading All-Purpose Vinyl Badge Holders - 3" x 4" - Clip Attachment - Clear</a><br>SKU:
<a class="ProductSKULabel" href="/Product/Top-Loading-All-Purpose-Vinyl-Badge-Holders---3-x-4---Clip-Attachment---Clear-HSE-6-C-CLR-96101.htm?cartprodid=28011557">HSE-6-C-CLR</a></td>
<td class="currency"><span class="ItemExtendedPriceLabel">37.00</span></td>
</tr>
<tr class="RowStyle" onmouseover="document.getElementById('w3_SurveyContainer28011561').style.display = '';" onmouseout="document.getElementById('w3_SurveyContainer28011561').style.display = 'none';">
<td class="ProductSKUColumn" align="left" colspan="2"><a class="ItemNameLabel" href="/Product/38-Adjustable-Polyester-Lanyard-with-Bulldog-Clip-LY-13-R-CC-38580.htm?cartprodid=28011561">Adjustable Polyester Lanyard with Bulldog Clip</a><br>SKU: <a class="ProductSKULabel" href="/Product/38-Adjustable-Polyester-Lanyard-with-Bulldog-Clip-LY-13-R-CC-38580.htm?cartprodid=28011561">LY-13-R-CC</a></td>
<td
class="currency"><span class="ItemExtendedPriceLabel">179.00</span></td>
</tr>
</tbody>
</table>
答案 0 :(得分:0)
尝试使用正则表达式How to use
var regex = /^vexillology$/i;
function getValue() {
return document.getElementById("myinput").value;
}
function test() {
alert(regex.test(getValue()));
}
function match() {
alert(getValue().match(regex));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="myinput" value="vexillology"/>
<button id="testBtn" onclick=test()>test</button>
<button id="matchBtn" onclick=match()>match</button>
答案 1 :(得分:0)
我不确定您使用的方法,因此我会提出另一种方法:
.filter() + .text() + .match()
HIH
var counter = $(".ProductSKULabel").filter(function(){
return $(this).text().match(/(HSE-)|(LY-)|(HS-)|(LY-)/)!==null;
}).length;
console.log(counter);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="w3_tblCartContents" class="CartTable" cellspacing="0" cellpadding="0" border="0" style="width:100%;border-collapse:collapse;">
<tbody>
<tr class=" CartHeader">
<th></th>
<th class="ProductSKUColumn" colspan="2">Product</th>
<th>Qty</th>
<th>Total</th>
</tr>
<tr class="AlternatingRowStyle">
<td class="ProductSKUColumn" align="left" colspan="2"><a class="ItemNameLabel" href="/Product/Top-Loading-All-Purpose-Vinyl-Badge-Holders---3-x-4---Clip-Attachment---Clear-HSE-6-C-CLR-96101.htm?cartprodid=28011557">Top-Loading All-Purpose Vinyl Badge Holders - 3" x 4" - Clip Attachment - Clear</a><br>SKU:
<a class="ProductSKULabel" href="/Product/Top-Loading-All-Purpose-Vinyl-Badge-Holders---3-x-4---Clip-Attachment---Clear-HSE-6-C-CLR-96101.htm?cartprodid=28011557">HSE-6-C-CLR</a></td>
<td class="currency"><span class="ItemExtendedPriceLabel">37.00</span></td>
</tr>
<tr class="RowStyle" onmouseover="document.getElementById('w3_SurveyContainer28011561').style.display = '';" onmouseout="document.getElementById('w3_SurveyContainer28011561').style.display = 'none';">
<td class="ProductSKUColumn" align="left" colspan="2"><a class="ItemNameLabel" href="/Product/38-Adjustable-Polyester-Lanyard-with-Bulldog-Clip-LY-13-R-CC-38580.htm?cartprodid=28011561">Adjustable Polyester Lanyard with Bulldog Clip</a><br>SKU: <a class="ProductSKULabel" href="/Product/38-Adjustable-Polyester-Lanyard-with-Bulldog-Clip-LY-13-R-CC-38580.htm?cartprodid=28011561">LY-13-R-CC</a></td>
<td
class="currency"><span class="ItemExtendedPriceLabel">179.00</span></td>
</tr>
</tbody>
</table>
&#13;
答案 2 :(得分:0)
您的每个语句都没有迭代选择器。我对它进行了修改,以便在每个内部进行评估。
var counter = 0;
$(".ProductSKULabel").each(function() {
if($(this).text().indexOf("LY-") >=0 || $(this).text().indexOf("LYE-") || $(this).text().indexOf("HSE-") ) {
counter++;
}
});