简体   繁体   English

contains()始终返回true

[英]contains() always returns true

I have a dynamically created table and when I add new row I need to check if a book already exists in the table or not. 我有一个动态创建的表,当我添加新行时,我需要检查表中是否已经存在一本书。

function drawRow(obj) {
    var row = $("<tr>")
    row.append($("<td><p>" + obj.Id + "&nbsp;&nbsp;</p></td>"));
    row.append($("<td><p>" + obj.bookno+ "&nbsp;&nbsp;</p></td>"));
    row.append($("<td><p>" + obj.isbn + "&nbsp;&nbsp;</p></td>"));

    if ($(".bktbl tr").contains == obj.isbn) { // It shows me always false. I think this is not working properly
        alert("Already there!");
    } else {
        $(".bktbl table").append(row);
    }
}

You need to use :contains() Selector along with length property 您需要使用:contains()选择器以及length属性

Select all elements that contain the specified text. 选择所有包含指定文本的元素。

 if($(".bktbl tr:contains('"+obj.isbn+"')").length)

instead of 代替

 if ($(".bktbl tr").contains == obj.isbn) {

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

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