개발일지/JavaScript + jquery

연관 배열 예제

연습용365 2021. 11. 6. 00:22

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <form name="myForm">
        필드1 <input type="text" name="a0"><br />
        필드2 <input type="text" name="a1"><br />
        필드3 <input type="text" name="a2"><br />
        <input type="button" value="초기화" onclick="init();">
    </form>
</body>
</html>

 

    <script>
        function init()
        {
           for(var i=0; i < 3; i++)
           {
               document.myForm["a" + i].value = i;
           }
        }
    </script>

 

document.myForm["a" + i].value = i;

연관배열은 반드시 따옴표 안에 들어가야한다.