2013-02-18 78 views
0

我有一些元素可以在文档中声明为可排序。后来在函数中,我使用toArray将它们放入数组中。我想要做的事情是遍历这个数组,并获得那些我正在寻找的具有特定元素的元素。我打开数组并使用.find,但它说元素myarray [index]没有方法查找,所以然后我尝试myarray.eq(index).find,然后它说他们没有方法eq。任何帮助,这将不胜感激。我的代码如下。对象没有方法eq或找到

var imageBlocks = $(".imageBlocks").sortable("toArray"); 
    images = Array(); 

    for(i = 0; i < imageBlocks.length ; i++) 
    { 
    image = imageBlocks.eq(i).find(".post_image"); 
    if(image.length > 0){ 
     images.push(image); 
    } 

    } 
+1

你的变量是'imageBlocks'不是'$ imageBlocks ' – 2013-02-18 17:50:13

+0

'imageBlocks!== $ imageBlocks' – 2013-02-18 17:50:38

回答

0

貌似toArrray()只返回一个字符串数组对应于该对象的id,而不是一个数组我曾经拥有过的物体本身。我解决了这个问题的代码更改为:

var imageBlocks = $(".imageBlocks").sortable("toArray"); 
    images = Array(); 

    for(i = 0; i < imageBlocks.length ; i++) 
    { 
    imageID = $("#"+imageBlocks[i]); 
    image = imageID.find(".post_image"); 

    if(image.length > 0){ 
     images.push(image); 
    }  
    } 
0

试试这个:

image = imageBlocks[i].find(".post_image"); 

和corrent $imageBlocks/imageBlocks

0

imageBlocks === $ imageBlocks

this should work: 

var imageBlocks = $(".imageBlocks").sortable("toArray"); 
    images = Array(); 

    for(i = 0; i < imageBlocks.length ; i++) 
    { 
    image = imageBlocks.eq(i).find(".post_image"); 
    if(image.length > 0){ 
     images.push(image); 
    } 

    } 
+0

糟糕,$是我尝试过的东西,然后忘记取出。 imageBlocks.eq(ⅰ).find( “post_image。”);仍然会产生错误“Uncaught TypeError:Object imageBlock1,imageBlock2,imageBlock3,imageBlock4,imageBlock5 has no method'eq'” – xxyyxx 2013-02-18 17:56:43

0

看你的范围有限,我不明白为什么这是行不通的:

var myimages = imageBlocks.filter(function(){ 
    return $(this).find('.post_Image').length >0; 
}); 
var myArray = myimages.toArray(); 

编辑:链接:

var myArray = imageBlocks.filter(function(){ 
    return $(this).find('.post_Image').length > 0; 
}).toArray(); 

上午我在这里错过了什么?

编辑:只是要清楚,用于你的例子;我上面的代码,在实际使用中扩展出来:

$(".imageBlocks").filter(function(){ 
    return $(this).find('.post_Image').length > 0; 
}).toArray(); 

我把小提琴和以上和一些标记,所以你可以看到它完全implimented:http://jsfiddle.net/kDNuc/