简体   繁体   English

Javascript遍历不起作用的元素

[英]Javascript to iterate through elements not working

I had a JavaScript working until a few minutes ago, now it's not working any more but I didn't change anything. 直到几分钟之前,我都在使用JavaScript,但现在它不再工作,但我没有做任何更改。

My JavaScript is: 我的JavaScript是:

var estadoactual ="";
for (var i=1;i<=document.getElementById("cantidaddepermisos").value;i++) {
var elemento = 'chk_' + i
if (document.getElementById(elemento).checked == 1) {
     estadoactual = estadoactual + elemento + "#" + "Si" + "|"
} 
else {
      estadoactual = estadoactual + elemento + "#" + "No" + "|"
      }
}
alert("Estado actual: " + estadoactual)

In my scenario I have several checkboxes (chk_n) and what I want to do is to iterate through them, I know they will have the format chk_ + a number, so I made that "for loop" 在我的场景中,我有几个复选框(chk_n),我想要做的是遍历它们,我知道它们的格式将为chk_ +一个数字,因此我将其设置为“ for loop”

This code was working fine, don't know why it stopped working, I'm awake for 3 days working on this project maybe I'm missing something so I need some more people to see and tell if there is something obviously wrong. 这段代码运行良好,不知道为什么它停止工作,我在该项目上工作了3天很清醒,也许我丢失了一些东西,所以我需要更多的人来查看并确定是否存在明显错误的地方。

maybe the trouble would be cache in javascript file. 也许麻烦是在javascript文件中缓存。

Try change the source to: 尝试将源更改为:

<script language="JavaScript" src="js/myscript.js?n=1"></script> 

It's remove cache browser for your javascript file 它已删除您的javascript文件的缓存浏览器

Look at your code : 看你的代码:

for (var i=1;i<=document.getElementById("cantidaddepermisos").value;i++) {

You're iterating on your input value that is by default a String. 您正在迭代默认为String的输入值。 Try to parseInt it : 尝试parseInt:

for (var i=1;i<=parseInt(document.getElementById("cantidaddepermisos").value);i++) {

Hope it will solve your problem. 希望它能解决您的问题。

Ok, got it figured out. 好,知道了。 Thanks a lot for trying to help me and taking your time for this. 非常感谢您尝试帮助我并抽出宝贵的时间。 I will explain what happened in case some one else needs this. 如果有人需要它,我将解释发生了什么。

My table was created dinamically, this means my codebehind is creating one with 2 for each "permission" you can select from the DB. 我的表是动态创建的,这意味着我的代码背后是为您可以从数据库中选择的每个“权限”创建一个2。 Those permissions are loaded in the DB by hand (customer requirement) and you need to put a numer to each one, mines went from 1 to 34 (that was today morning when everything was working) then I added a new profile (99) by a special hidden permission. 这些权限是手动加载到数据库中的(客户要求),您需要为每个权限添加一个数字,地雷从1增至34(今天早上一切正常),然后我添加了一个新配置文件(99)特殊的隐藏权限。

This means there are 34 permissions in the DB so the for will loop from 1 to 34, but my checkboxes are named chk_ + "numberofpermission" 这意味着数据库中有34个权限,因此for将从1循环到34,但是我的复选框名为chk_ +“ numberofpermission”

This means they are chk_1 to chk_34 AND chk_99 at the end, so when the javascript reached the 34 posittion it was looking for chk_34 wich didn't exist because the name of the checkbox is "chk_99" 这意味着它们最后是chk_1到chk_34和chk_99,因此当javascript到达34位置时,它正在查找的chk_34不存在,因为复选框的名称是“ chk_99”

Really hard to figure this out but now it's working again, will need to change the way I set this hidden permission to the users to avoid this problem. 确实很难弄清楚,但是现在它又可以工作了,将需要更改我为用户设置此隐藏权限的方式,以避免出现此问题。

Thanks a lot for the time, this community it's really outstanding. 非常感谢您提供的时间,这个社区真的很棒。

Greetings from Argentina 来自阿根廷的问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM