正确使用“for ... in”在JavaScript中循环? [英] Correct use of a "for...in" loop in javascript?

查看:89
本文介绍了正确使用“for ... in”在JavaScript中循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我提出问题之前,我想让每个人都知道我很欣赏这样一个事实:总有一个人愿意提供帮助,而在我结束时,我会尽可能多地回馈社区。谢谢



现在,我想了解如何正确利用JavaScript中的for ... in循环,我已经做了一些研究并尝试了一些事情,但我仍然不清楚如何正确使用它。



假设我在HTML表单中有一个随机数量的select标签,我不要求用户为所有这些选项选择一个选项,如果需要,可以保留一些选项。
但是我需要知道他们是否选择了一个或至少一个。



我试图找出用户是否选择其中任何一个是通过使用for ... in循环。例如:

  var allSelected = $(select option:selected); 
var totalSelected = $(select option:selected)。length;

第一个变量生成一个包含所有选定选项的数组。
第二个变量告诉我在表单中有多少选定的选项(选择标签可以多于一个,每次都会更改)。
现在,为了查看是否有任何选择,我遍历每个元素(选择的选项),并检索value属性。默认的option标签有一个值=0,所以如果任何选中的选项返回一个大于0的值,我知道至少选择了一个选项,但它不必按顺序排列,这是我的循环到目前为止:

  for(var i = 0; i< totalSelected; i ++){
var everyOption = $ (allSelected [I])VAL();
var defaultValue = 0;
if(eachOption == defaultValue){
...重定向到另一个页面
}否则if(eachOption> defaultValue){
...我显示警告
}
}

我的问题是,只要if匹配一个0值,它将用户发送到下一页而不测试数组中的其余元素,用户可以选择第二个或第三个选项。



我真正想要做的是检查数组中的所有元素然后采取下一步行动,在我看来这是我能做到的,但我没有做对:

  var randomValue = 25; 
for(allSelected中的randomValue){
var found = true;
var notFound = false
if(found){
显示警告
}否则{
重定向到下一页
}
}

这个循环或我使用的逻辑是有缺陷的(我很确定),我想要的要做的是针对单个变量测试数组中的所有元素,并相应地采取下一个操作。



我希望这对你们有所帮助,任何帮助都会是谢谢,

谢谢,

JC

解决方案

更简单的写作方式(我认为你尝试使用 .each()

  //这是要知道我们是否要重定向
var redir = true;
$(select option:selected)。each(function(){
var val = $(this).val();

if(val> 0 ){
alert('找到一个!');
}
if(val!= 0){
redir = false;
//你可以返回false这里如果你想停止处理每个循环!
}
});

if(redir){
window.location =/ nextpage;
}


before I ask my question I wanted to let everybody know that I appreciate the fact that there's always somebody out there willing to help, and on my end I'll try to give back to the community as much as I can. Thanks

Now, I would like to get some pointers as to how to properly take advantage of the "for...in" loop in JavaScript, I already did some research and tried a couple things but it is still not clear to me how to properly use it.

Let's say I have a random number of "select" tags in an HTML form, and I don't require the user to select an option for all of them, they can leave some untouched if they want. However I need to know if they selected none or at least one.

The way I'm trying to find out if the user selected any of them is by using the "for...in" loop. For example:

var allSelected = $("select option:selected");
var totalSelected = $("select option:selected").length;

The first variable produces an array of all the selected options. The second variable tells me how many selected options I have in the form (select tags could be more than one and it changes every time). Now, in order to see if any has been selected I loop through each element (selected option), and retrieve the "value" attribute. The default "option" tag has a value="0", so if any selected option returns a value greater than 0, I know at least one option has been selected, however it does not have to be in order, this is my loop so far:

for(var i = 0; i < totalSelected; i++){
  var eachOption = $(allSelected[i]).val();
  var defaultValue = 0;
  if(eachOption == defaultValue){
    ...redirect to another page
  }else if(eachOption > defaultValue){
    ... I display an alert
  }
}

My problem here is that as soon as the "if" matches a 0 value, it sends the user to the next page without testing the rest of the elements in the array, and the user could have selected the second or third options.

What I really want to do is check all the elements in the array and then take the next action, in my mind this is how I could do it, but I'm not getting it right:

var randomValue = 25;  
for(randomValue in allSelected){
  var found = true;
  var notFound = false
  if(found){
    display an alert
  }else{
    redirect to next page
  }
}

This loop or the logic I'm using are flawed (I'm pretty sure), what I want to do is test all the elements in the array against a single variable and take the next action accordingly.

I hope this makes some sense to you guys, any help would be appreciated.
Thanks,
JC

解决方案

A simpler way to write (what I think) your trying to achieve using .each()

// this is to know if we want to redirect
var redir = true;
$("select option:selected").each(function() {
  var val = $(this).val();

  if (val > 0) {
    alert('Found one!');
  }      
  if (val != 0) { 
    redir = false;
    // you can return false here if you want to stop processing the each loop too!
  } 
});

if (redir) {
  window.location = "/nextpage";
}

这篇关于正确使用“for ... in”在JavaScript中循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆