需要帮助在循环中将多个数组值设置为 null - javascript
我一直致力于创建一个自定义脚本来帮助管理登录页面的秘密问题表单。我试图使所有单独的选择列表都是动态的,因为如果用户选择其中一个问题,则它将不再是其余选择列表中的选项,依此类推。无论如何,我遇到的问题是当我尝试将其他列表中的变量设置为空时。我目前只处理 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑到每个数组中的位置可能会有所不同,我在这里没有看到太多选择。
在单独的循环中执行此操作,除非您在两个数组中重复值并共享相同的位置
EDTI 我想出了一个简单的解决方案,它可能有效,创建一个函数。一个接收数组作为参数的函数怎么样?
像这样:
我想我表达了我的观点,也许它可以带你走向正确的方向
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:
I think i make my point, perhaps it can take you in the right direction
我敢打赌,代码不会到达 array3[7],因为它不存在,或者
array2
太短,并且您会收到 JavaScript 异常这会阻止代码进行检查。array2
和array3
是否可能比array1
短?这是更多的代码,但我会这样做:
My bet is that the code isn't getting to
array3[7]
because either it doesn't exist or thatarray2
is too short and you're getting a JavaScript exception that's stopping the code from doing the check. Is it possible thatarray2
andarray3
are shorter thanarray1
?It is more code, but I would do it like this: