简体   繁体   中英

jquery. how to equal .text() .val()

I need to equal two value, but it's not work..

var rowID = $("#@idSelectObjectGuid").val();

$($(".ls-grid-body tr").children(".@vcTable.PrimaryKey")).each(function () {

   alert($(this).text() + " == " + rowID);

   if ($(this).text() == rowID) {

       $(".ls-grid-body tr").removeClass("lst-state-selected");
       $(this).addClass("lst-state-selected");

   }
});

alert result:

df5ebd84-14a1-4e57-9f38-32d4e84e1503 - 41 == df5ebd84-14a1-4e57-9f38-32d4e84e1503 - 36

Why first lenght is 41 and why they not equal...

How to do it?

try this : use .trim() as there could be a space before / after the text

var rowID = $("#@idSelectObjectGuid").val().trim();

$($(".ls-grid-body tr").children(".@vcTable.PrimaryKey")).each(function () {

   alert($(this).text() + " == " + rowID);

   if ($(this).text().trim() == rowID) {

       $(".ls-grid-body tr").removeClass("lst-state-selected");
       $(this).addClass("lst-state-selected");

   }
});

This is the sort of thing that filter is good for:

$(".ls-grid-body tr").children(".@vcTable.PrimaryKey").filter(function () {
   return $.trim($(this).text()) == rowID;
}).addClass("lst-state-selected");

Notes:

  • string.trim() is not available on all browsers, so use $.trim() instead
  • You seem to have a redundant $() wrapper.
  • You seem to have a mismatch between the elements you removeClass and addClass to (tr vs tds).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM