简体   繁体   English

在jquery each()迭代器函数中访问JS对象

[英]Access JS object inside jquery each() iterator function

$('input.ISSelectSearch').each(function(i) {
    var box = new Object;
    box.size = 80;
    box.width = 110;
    //CODE CODE CODE
});

How can I access the value of box which was set in the previous iteration? 如何访问上一次迭代中设置的box的值? Alternatively, is it possible to access it via a key of some kind? 或者,是否可以通过某种键访问它?

$('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
}); 

The problem is that I need to know the data associated with each 'input.ISSelectSearch' element box so that I can change them depending on the values of the current or preceding box objects. 问题是我需要知道与每个'input.ISSelectSearch'元素框关联的数据,以便我可以根据当前或前面的框对象的值来更改它们。 In other words I need a connection between the element box objects so that I can specify certain changes in one based on the values of another. 换句话说,我需要元素框对象之间的连接,以便我可以根据另一个的值指定一个更改。

you can do something like this 你可以做这样的事情

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
}); 

EDIT AFTER THE COMMENT:- 编辑后编辑: -
probably you wanna use $.data() method 可能你想使用$ .data()方法
this was you can associate the data with the element 这是您可以将数据与元素相关联
so inside the loop you can do 所以在循环内你可以做到

$('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)
});

You can do it with index if you like 如果您愿意,可以使用索引来完成

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
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM