2014-11-02 35 views
0

所有,功能查找在TD元素文本不工作

我看到的所以這裏幾種方法,我怎麼能找到的文本或HTML,寫在表格中的TD元素。出於某種原因,他們似乎沒有爲我工作。我顯然在做一些非常錯誤的事情,但我無法弄清楚。

編輯:問題是從我的TD總是顯示爲未定義的HTML()。我似乎無法使用html(),text()等獲取文本(EG company0)。

這是我的功能。 #searchbox是輸入類型:文本

$(document).ready(function() { 
$('#searchbox').change(function() { 
    var searchText = $(this).val(); 
    $('.prospect_table tr').each(function() { 
     var obj = $(this).find('.propsect_td'); 
     if (typeof obj != 'undefined') { 
      if (hideText(obj.html(), searchText)) 
       $(this).show(); 
      else 
       $(this).hide(); 
     } 
    }); 
}); 
}); 

function hideText(prospectName, text) { 
    if (prospectName == 'undefined') 
     return false; 

    if (prospectName.toLowerCase().indexOf(text.toLowerCase()) >= 0) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

這裏是我的源頁面

<input type="text" name="txtSearch" id="searchbox" value="Begin typing here..." /> 

<table class="prospect_table"> 
<tr> 
    <th> 
     ProspectName 
    </th> 
    <th> 
     Inactive 
    </th> 
    <th></th> 
</tr> 

<tr> 
    <td class="prospect_td"> 
     Company0 
    </td> 
    <td> 
     <input class="check-box" disabled="disabled" type="checkbox" /> 
    </td> 
    <td> 
     <a href="/CrmWeb/Company/Edit/0">Edit</a> | 
     <a href="/CrmWeb/Company/Details/0">Details</a> | 
     <a href="/CrmWeb/Company/Delete/0">Delete</a> 
    </td> 
</tr> 
<tr> 
    <td class="prospect_td"> 
     Company1 
    </td> 
    <td> 
     <input class="check-box" disabled="disabled" type="checkbox" /> 
    </td> 
    <td> 
     <a href="/CrmWeb/Company/Edit/0">Edit</a> | 
     <a href="/CrmWeb/Company/Details/0">Details</a> | 
     <a href="/CrmWeb/Company/Delete/0">Delete</a> 
    </td> 
</tr> 

等等

我如何能改善這一點,並使其工作有什麼建議?也許我需要更多的id標籤,而不是類?

感謝您的任何幫助或建議!

+1

'prospectName.toLowerCase()。的IndexOf( ...'請花時間在您的開發者控制檯中觀察錯誤 – 2014-11-02 19:01:10

+0

@squint我所得到的就是那個prospectName是undefined – Carson 2014-11-02 19:03:09

+0

這就是它的意思嗎?您的'IndexOf'應該是'indexOf'。 – 2014-11-02 19:05:04

回答

1

試試這個。 Changed'.propsect_td '到' .prospect_td」, '的IndexOf' 到 '的indexOf' 和替代的typeof OBJ!= '未定義' 與obj.length!= 0

$(document).ready(function() { 
$('#searchbox').change(function() { 
    var searchText = $(this).val(); 
    $('.prospect_table tr').each(function() { 
     debugger 
     var obj = $(this).find('.prospect_td'); 
     if (obj.length != 0) { 
      if (hideText(obj.html(), searchText)) 
       $(this).show(); 
      else 
       $(this).hide(); 
     } 
    }); 
}); 
}); 

function hideText(prospectName, text) { 
    debugger 
    if (prospectName == 'undefined') 
     return false; 

    if (prospectName.toLowerCase().indexOf(text.toLowerCase()) >= 0) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
0
$(document).ready(function() { 
$('#searchbox').change(function() { 
    var searchText = $(this).val(); 
    $('.prospect_table tr').each(function() { 
     var obj = $(this).find('.prospect_td'); 

     console.log(obj.text()); 
     if (typeof obj != 'undefined') { 
      if (hideText(obj.text(), searchText)) 
       $(this).show(); 
      else 
       $(this).hide(); 
     } 
    }); 
}); 
}); 

function hideText(prospectName, text) { 
    if (prospectName == 'undefined') 
     return false; 

    if (prospectName.toLowerCase().indexOf(text.toLowerCase()) >= 0) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

很少有錯別字固定...而不是html()我使用text()屬性。小提琴:http://jsfiddle.net/bn1403nk/

+0

不幸的是,這沒有奏效。但是,謝謝。 – Carson 2014-11-02 19:19:07

+0

它是在小提琴內部工作,請重新檢查 - >您的活動正在改變,如果您想立即改變,請使用鍵盤...... – sinisake 2014-11-02 19:20:26

+0

我會將其更改爲keyup,因爲我確實需要即時檢查。 – Carson 2014-11-02 19:21:15