2013-02-18 119 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/