最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery filter not working with jquery object - Stack Overflow

programmeradmin2浏览0评论

I using ajax to retrieve a web page like that:

   $.ajax({
            url: "/Cadastros/pagina_busca_correios?cep=" + cep,
            type: "Get",
            DataType: 'Html',
            success: function (data) {                       
                 var webdata = data;
                 var tables = $(webdata).filter('table');
                 alert(tables);                                                
            }
   });

I am able to print the html in data return but after creating the jquery object and apply filter I get noting. It is like a have an error with my javascript.

PS. I know I have 4 tables inside this html string, so I would like to filter the tables and iterate through them to perform actions. Those elements inside the DOM has no id´s or names that I could use to select straight forward.

What I am doing wrong?

I using ajax to retrieve a web page like that:

   $.ajax({
            url: "/Cadastros/pagina_busca_correios?cep=" + cep,
            type: "Get",
            DataType: 'Html',
            success: function (data) {                       
                 var webdata = data;
                 var tables = $(webdata).filter('table');
                 alert(tables);                                                
            }
   });

I am able to print the html in data return but after creating the jquery object and apply filter I get noting. It is like a have an error with my javascript.

PS. I know I have 4 tables inside this html string, so I would like to filter the tables and iterate through them to perform actions. Those elements inside the DOM has no id´s or names that I could use to select straight forward.

What I am doing wrong?

Share Improve this question asked Jul 1, 2013 at 21:08 Guilherme LongoGuilherme Longo 6553 gold badges9 silver badges20 bronze badges 4
  • 1 Are table elements in the root of webdata? – Claudio Redi Commented Jul 1, 2013 at 21:12
  • 2 It's hard to say what you're doing wrong without seeing the actual returned html. I suspect you want find instead of filter but I have no way to know. – James Montagne Commented Jul 1, 2013 at 21:12
  • Are your tables wrapped in another tag if so then use find instead of filter – PSL Commented Jul 1, 2013 at 21:12
  • Note that you'd normally write the dataType in lowercase and the type in uppercase, not that it would matter as jQuery sorts it out, but using uppercase for the first letter only is bad form, at least a little bit, same goes for assigning new variables that does the exact same thing as the original one. – adeneo Commented Jul 1, 2013 at 21:15
Add a ment  | 

1 Answer 1

Reset to default 8

filter() doesn't find child elements, find() does. To make it sure it works either way, append the data to a new element and use find() :

var tables = $('<div />').append(data).find('table');

and you can use data directly, no need for another variable.

发布评论

评论列表(0)

  1. 暂无评论