简体   繁体   English

jQuery的。 如何等于.text().val()

[英]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... 为什么第一长度是41,为什么他们不相等...

How to do it? 怎么做?

try this : use .trim() as there could be a space before / after the text 试试这个:使用.trim()因为文本前后可能会有空格

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: 这是filter擅长的一类事情:

$(".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 string.trim()并非在所有浏览器上都可用,因此请改用$.trim()
  • You seem to have a redundant $() wrapper. 您似乎有一个多余的$()包装器。
  • You seem to have a mismatch between the elements you removeClass and addClass to (tr vs tds). 您在removeClassaddClass的元素之间似乎不匹配(tr vs tds)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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