a1="1",a2="2";
for (i=1;i<=2;alert(eval("a"+i++)))

解决方案 »

  1.   

    检查js的代码并执行
    eval(str)
      

  2.   

    eval MethodSee Also
    String ObjectApplies To: Global Object
    Requirements
    Version 1
    Evaluates JScript code and executes it. eval(codeString) 
    The required codeString argument is a string value that contains valid JScript code. This string is parsed by the JScript parser and executed.Res
    The eval function allows dynamic execution of JScript source code. For example, the following code creates a new variable mydate that contains a Date object: eval("var mydate = new Date();");
    The code passed to the eval method is executed in the same context as the call to the eval method.
      

  3.   

    eval();是个全局方法,它将执行参数中的javascript代码字符串,并返回最后一个语句的返回值。如果没有返回值就返回undefined.
    使用方法:
    楼上的....
      

  4.   

    <input type="text" name="test1">
    <input type=button onclick="check()">
    <script>
    function check(){
    var obj="test"+1;   
    alert(obj.value);//代碼執行失敗
    var obj=eval("test"+1);
    alert(obj.value);//代碼執行成功
    }
    </script>
      

  5.   

    function a()
    {
    eval("x")();
    }
    function x()
    {
    alert("xxxx");
    }