2012-07-10 64 views
1
$('input.ISSelectSearch').each(function(i) { 
    var box = new Object; 
    box.size = 80; 
    box.width = 110; 
    //CODE CODE CODE 
}); 

如何訪問在前一次迭代中設置的box的值?或者,是否有可能通過某種類型的密鑰訪問它?在jQuery中訪問JS對象each()迭代器函數

$('input.ISSelectSearch').each(function(i) { 
    var box = new Object; 
    box.size = 80; 
    box.width = 110; 
    prevsize = $(this).box[/* previous iteration element id or name */].size 
    //CODE CODE CODE 
}); 

的問題是,我需要知道與每個'input.ISSelectSearch'元件盒相關聯的數據,這樣我可以取決於當前或先前框對象的值改變它們。換句話說,我需要元素框對象之間的連接,以便我可以根據另一個對象的值指定某些更改。

回答

4

你可以像這樣

var pre = null; 
$('input.ISSelectSearch').each(function(i) { 
    var box = new Object; 
    box.size = 80; 
    box.width = 110; 
    //condition so that first time it dosent shows an error 
    if(pre!=null){ 
    //CODE 

    } 
    pre = this; 
    //CODE CODE CODE 
}); 

編輯註釋後面的東西: -
也許你想使用$。數據()方法
這是你可以將數據與元素
所以關聯在循環中,你可以做

$('input.ISSelectSearch').each(function(i) { 
    var box = new Object; 
    box.size = 80; 
    box.width = 110; 
    $('input.ISSelectSearch').data(i,box); 
    //and now you can retrive the data whenevr you want 
    var olderObject = $('input.ISSelectSearch').data(SOME_OLDER_INDEX) 
}); 
+0

你確定你想要條件pre!= null嗎?在哪個迭代將不是null? – JeffS 2012-07-10 14:59:06

+0

我想你想'pre = this'outside if if – mariusnn 2012-07-10 14:59:48

+0

@mariusnn - thankx – 2012-07-10 15:00:50

0

你可以,如果你喜歡

與指數做
var $This = $('input.ISSelectSearch'); 
$This.each(function (index, value) { 
    var Pre; 
    var box = { size: 80, width: 110 }; 
    if (index > 0) { 
    Pre = $This[index - 1]; 
    //Your code hear 
    } else { 
    //otherwise 
    } 
});