开发者

Javascript var name counting variable

开发者 https://www.devze.com 2023-04-03 20:05 出处:网络
I have the following bit of code: function finalCheck(theForm) { var z = 0; for(var i=0;i<15;i++){ var _i = theForm.elements[i].value;

I have the following bit of code:

function finalCheck(theForm) {
var z = 0;
for(var i=0;i<15;i++){
    var _i = theForm.elements[i].value;
    if(_i == ""){
        theForm.elements[i].style.background = '#FFD6D6';
        开发者_运维知识库z = 1;
    }
}
if(z == 1){
    alert("Please correct the fields highlighted in red");
    return false;
} else {
    return true;
}
}

What I was attempting to do was set the name of var _i to var _ and then the index that the counting variable was currently on. For example, _1 _2 _3 etc. Any way to do this?


So I under stand what you are trying to do, and your solution is to use eval

eval("var _" + i + "= theForm.elements[i].value;");

Anyways your code doesn't need any dynamic generation of variable names as var _i is a local variable. I hope you are asking this question for learning purpose.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号