J'ai besoin d'augmenter un compteur si certains mots existent dans une table utilisant jQuery. Mon compteur n'augmente que de 1 lorsque je m'attends à ce qu'il augmente de 2.

HTML et jQuery ci-dessous:

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
Liza Kline 11 avril 2018 à 17:34

3 réponses

Meilleure réponse

Je ne suis pas sûr de l'approche que vous utilisez, je vais donc vous présenter une alternative:

.filter () + . text () + .match ()

HIH

var counter = $(".ProductSKULabel").filter(function(){
  return $(this).text().match(/(HSE-)|(LY-)|(HS-)|(LY-)/)!==null;
}).length;

console.log(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
Scaramouche 11 avril 2018 à 15:06

Essayez d'utiliser l'expression régulière Comment utiliser

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>
0
Jakub Kowalski 11 avril 2018 à 14:45

Votre chaque énoncé n'itère pas sur un sélecteur. Je l'ai modifié pour que l'évaluation soit effectuée à l'intérieur de chacun.

var counter = 0;

$(".ProductSKULabel").each(function() {
    if($(this).text().indexOf("LY-") >=0 || $(this).text().indexOf("LYE-") || $(this).text().indexOf("HSE-") ) {
        counter++;
    }
});
0
Guerrerito 11 avril 2018 à 15:17