需要帮助在循环中将多个数组值设置为 null - javascript

发布于 2024-10-21 21:22:05 字数 581 浏览 3 评论 0原文

我一直致力于创建一个自定义脚本来帮助管理登录页面的秘密问题表单。我试图使所有单独的选择列表都是动态的,因为如果用户选择其中一个问题,则它将不再是其余选择列表中的选项,依此类推。无论如何,我遇到的问题是当我尝试将其他列表中的变量设置为空时。我目前只处理 3 个列表,因此我查看一个列表,然后在其他 2 个列表中查找/删除匹配项。这是我删除任何匹配项的循环。

for(i=0; i<array1.length; i++) {
    if(array2[i].value == txtbox1.value) {
        document.questions.questions2.options[i] = null
    }
    if(array3[i].value == txtbox1.value) {
        document.questions.questions3.options[i] = null
    }
}

如果两个匹配项位于数组中相同的值/位置,则效果很好。但是,例如,如果一个匹配项位于 array1[1],另一个匹配项位于 array3[7],则仅删除第一个匹配项,而不删除第二个匹配项。我有什么遗漏的吗?任何帮助表示赞赏。谢谢!

I have been working on creating a custom script to help manage a secret questions form for a login page. I am trying to make all the seperate select lists dynamic, in that if a user selects a question in one, it will no longer be an option in the rest, and so on. Anyways, the problem I am having is when I try to set the variables in the other lists to null. I am currently working with only 3 lists, so I look at one list, and find/delete matches in the other 2 lists. Here is my loop for deleting any matches.

for(i=0; i<array1.length; i++) {
    if(array2[i].value == txtbox1.value) {
        document.questions.questions2.options[i] = null
    }
    if(array3[i].value == txtbox1.value) {
        document.questions.questions3.options[i] = null
    }
}

This works fine if both the matches are located at the same value/position in the array. But if one match is at array1[1] and the other match is at array3[7] for example, then only the first match gets deleted and not the second. Is there something I am missing? Any help is appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹊巢 2024-10-28 21:22:05

考虑到每个数组中的位置可能会有所不同,我在这里没有看到太多选择。

在单独的循环中执行此操作,除非您在两个数组中重复值并共享相同的位置

EDTI 我想出了一个简单的解决方案,它可能有效,创建一个函数。一个接收数组作为参数的函数怎么样?

像这样:

function finder(var array[], var valueToFound, var question) {
     for (i=0; i<array.lenght; i++) {
          if (array[i].value == valueToFound) {
                switch (question) {
                      case 1: document.questions.questions1.options[i] = null;
                              break;
                }

                return;
          }
     }
}

我想我表达了我的观点,也许它可以带你走向正确的方向

I don't see too many choices here, considering that the position in each array can vary.

Do it in separate loops, unless of course you repeat values in both arrays and share the same position

EDTI I figured out a simple solution, it may work, create a function. How about a function wich recives an array as parameter.

Something like this:

function finder(var array[], var valueToFound, var question) {
     for (i=0; i<array.lenght; i++) {
          if (array[i].value == valueToFound) {
                switch (question) {
                      case 1: document.questions.questions1.options[i] = null;
                              break;
                }

                return;
          }
     }
}

I think i make my point, perhaps it can take you in the right direction

扛起拖把扫天下 2024-10-28 21:22:05

我敢打赌,代码不会到达 array3[7],因为它不存在,或者 array2 太短,并且您会收到 JavaScript 异常这会阻止代码进行检查。 array2array3 是否可能比 array1 短?

这是更多的代码,但我会这样做:

var selectedvalue == txtbox1.value;
for(i=0; i<array2.length; i++) { // iterate over the length of array2, not array1
    if(array2[i].value == selectedvalue) {
        document.questions.questions2.options[i] = null;
        break; // found it, move on
    }
}
for(i=0; i<array3.length; i++) {
    if(array3[i].value == selectedvalue) {
        document.questions.questions3.options[i] = null;
        break; // you're done
    }
}

My bet is that the code isn't getting to array3[7] because either it doesn't exist or that array2 is too short and you're getting a JavaScript exception that's stopping the code from doing the check. Is it possible that array2 and array3 are shorter than array1?

It is more code, but I would do it like this:

var selectedvalue == txtbox1.value;
for(i=0; i<array2.length; i++) { // iterate over the length of array2, not array1
    if(array2[i].value == selectedvalue) {
        document.questions.questions2.options[i] = null;
        break; // found it, move on
    }
}
for(i=0; i<array3.length; i++) {
    if(array3[i].value == selectedvalue) {
        document.questions.questions3.options[i] = null;
        break; // you're done
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文