开发者

get jquery :contains values into an array

I've got a table and want to 开发者_如何学运维get an array of cell values which contain a query provided by the user.

For example, the user inputs '20', and the table is

120
240
220
110

I want an array to contain

[120, 220]

I currently have

var data_list = jQuery('td:contains('+seach_val+')','table#data');

and I've also tried the above with .text() to see if that would get me the text, but none of this seems to work.

Should be pretty simple I thought, but I'm having some trouble with this.


If you are looking to create an array, the following code works:

<table id="data">
    <tr><td>120</td></tr>
    <tr><td>240</td></tr>
    <tr><td>350</td></tr>
    <tr><td>220</td></tr>
</table>

search_val = 20;
data_list = [];
jQuery('td:contains('+search_val+')','table#data').each(function(){
    data_list.push($(this).text());
});
console.log(data_list);

This outputs in the console:

["120", "220"]

http://jsfiddle.net/Mj5G4/4/


Try this and let me know if the following works/not:

var data_list = jQuery("td:contains('"+seach_val+"')","table#data");

Notice the quotation ' before the closing ". I remember I had a similar problem in the past, and I am looking to help you solve it as well :) hope that helps.


Can't you just loop through like this?

var searchString = "20";
var results = [];
$('td').each( function(index, element){
    if($(element).text().indexOf(searchString) != -1){
     results.push($(element).text());   
    }
});
alert(results);

http://jsfiddle.net/XZWEh/1/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜