Par*_*roX 46 javascript jquery
我想用来return false
打破.each()但同时返回一个值.我怎样才能做到这一点?
请参考解决方法以查看我要执行的操作:
function HasStores(state) {
var statehasstores = false;
$(stores).each(function (index, store) {
if (state == store.state && store.category == "meyers") {
statehasstores = true;
return false; // break
}
});
return statehasstores;
}
Run Code Online (Sandbox Code Playgroud)
在伪代码中我喜欢做的是:
Function () {
for() {
if found {
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
Šim*_*das 44
你这样做了......
来自http://api.jquery.com/each/的报价
"我们可以通过返回false来停止回调函数中的循环."
mhi*_*tza 15
要有创意:
try {
$(stores).each(function (index, store) {
if(state == store.state && store.category == "meyers"){
throw store;
}
});
}
catch(e) {
// you got e with the value
}
Run Code Online (Sandbox Code Playgroud)
不,我只是在开玩笑,不要用这个:).这是我喜欢分享的想法.
Bru*_*oLM 12
在循环外部使用变量来获取值并在之后使用它.
var value;
$(stores).each(function (index, store) {
if(state == store.state && store.category == "meyers"){
statehasstores = true;
value = store; // added line
return false; //break
}
});
alert(value);
Run Code Online (Sandbox Code Playgroud)
你做的方式很好.我在jsFiddle上测试过,请看这里的一个例子.
它不适合你吗?你能展示更多背景吗?
归档时间: |
|
查看次数: |
58960 次 |
最近记录: |