{
"infosList":[
{ "infoid":"1212280940407341", "fromchannelcode":"A03001001"
}
,
{ "infoid":"1212280940394684", "fromchannelcode":"A03001001"
}
,
{ "infoid":"1212280940399685", "fromchannelcode":"A03001001"
}
,
{ "infoid":"1212280940388903", "fromchannelcode":"A03001001"
}
]
}
我得到了其中一元素的infoid比如是"1212280940407341",那么在jquery中怎么根据当前的这个元素值得到它下一个元素的值呢,在网上看了用next()方法,但是不知道具体的写法,各位帮帮忙啊
5 个解决方案
#1
var o={
"infosList":[
{ "infoid":"1212280940407341", "fromchannelcode":"A03001001"
}
,
{ "infoid":"1212280940394684", "fromchannelcode":"A03001001"
}
,
{ "infoid":"1212280940399685", "fromchannelcode":"A03001001"
}
,
{ "infoid":"1212280940388903", "fromchannelcode":"A03001001"
}
]
},id='1212280940407341'
function getNext(o,id){
for(var i=0;i<o.length;i++){
if(o[i].infoid==id)return o[i+1];
}
return false;
}
var n=getNext(o.infosList,id);
if(n)alert(n.infoid)
else alert('没有下一项');
#2
谢谢版主,按您的方法已经解决了,再问下,如果用jquery的话,该怎么写呢,能用jquery的next()方法吗
#3
对dom行,对json对象选择器没用吧,可以each遍历
var o={
"infosList":[
{ "infoid":"1212280940407341", "fromchannelcode":"A03001001"
}
,
{ "infoid":"1212280940394684", "fromchannelcode":"A03001001"
}
,
{ "infoid":"1212280940399685", "fromchannelcode":"A03001001"
}
,
{ "infoid":"1212280940388903", "fromchannelcode":"A03001001"
}
]
},id='1212280940407341'
var n=false;
$(o.infosList).each(function(idx,item){
if(item.infoid==id){n=o.infosList[idx+1];return false;}
})
if(n)alert(n.infoid)
else alert('没有下一项');
#4
用下标可以不可以啊。
#5
版主的方法就是用下标呀。关键是你要找到当前相等的,才好定位下标。
#1
var o={
"infosList":[
{ "infoid":"1212280940407341", "fromchannelcode":"A03001001"
}
,
{ "infoid":"1212280940394684", "fromchannelcode":"A03001001"
}
,
{ "infoid":"1212280940399685", "fromchannelcode":"A03001001"
}
,
{ "infoid":"1212280940388903", "fromchannelcode":"A03001001"
}
]
},id='1212280940407341'
function getNext(o,id){
for(var i=0;i<o.length;i++){
if(o[i].infoid==id)return o[i+1];
}
return false;
}
var n=getNext(o.infosList,id);
if(n)alert(n.infoid)
else alert('没有下一项');
#2
谢谢版主,按您的方法已经解决了,再问下,如果用jquery的话,该怎么写呢,能用jquery的next()方法吗
#3
对dom行,对json对象选择器没用吧,可以each遍历
var o={
"infosList":[
{ "infoid":"1212280940407341", "fromchannelcode":"A03001001"
}
,
{ "infoid":"1212280940394684", "fromchannelcode":"A03001001"
}
,
{ "infoid":"1212280940399685", "fromchannelcode":"A03001001"
}
,
{ "infoid":"1212280940388903", "fromchannelcode":"A03001001"
}
]
},id='1212280940407341'
var n=false;
$(o.infosList).each(function(idx,item){
if(item.infoid==id){n=o.infosList[idx+1];return false;}
})
if(n)alert(n.infoid)
else alert('没有下一项');
#4
用下标可以不可以啊。
#5
版主的方法就是用下标呀。关键是你要找到当前相等的,才好定位下标。