Jquery $.each cannot terminate the loop

problem description: when the input content is the same as the SKU value in the list, the number of checks is increased by one, and when the input content does not match the sku in the table, the pop-up "check no this item information" pops up. Why sometimes the if and else parts will pop up, and sometimes it will be normal. Asking for advice using the "global variable" flag is not valid, ask for advice

/ / enter
$(".conversation _ review") .click (function () {

$("tbody tr td:nth-child(2)").each(function(index,val){
  if($(this).html()==$(".nav-input").val()){
    var count = $(this).parent().children().eq(-2).html();
    countPP;
    $(this).parent().children().eq(-2)[0].innerHTML= count;
  }else{
    layer.msg("",{icon:5});
  }

});

});

Feb.15,2022

if and else whichever is executed, as long as there is an exception, it is obvious that there is something wrong with your judgment. Do you still need to ask?
first of all, you should use a variable outside to receive the value of $('.nav-input'). Val () ) instead of using it directly in the loop, because this will cause multiple loops to get the dom element, which does not perform well.


this code is really boring.
$('.nav-input'). Val () should be assigned outside the each, because it takes a lot of resources to get the DOM object

By the way, please explain what
$(this). Parent (). Children (). Eq (- 2). Html ()
means. Gets the penultimate sibling element of the current element? Siblings, learn about

in addition, since jquery, is used, assigning values to html should try not to use innerHTML, but should use .html (count)

.

the most important point is that the
table should be rendered according to the data source, and the data should be looked up in the data source. After modifying the data, the table should be re-rendered according to the data

Menu