日期:2014-05-16  浏览次数:20475 次

js动态添加一组输入框方法二
<html>
    <head>
        <script language="javascript">
            var i = 1 ;
            function addInput(){
                var div = document.getElementById("inputDiv") ;
                var str = "<input type='text' id='id1"+i+"' name='name1"+i+"'>" ;
                str += "&nbsp;" ;
                str += "<input type='text' id='id2"+i+"' name='name2"+i+"'>" ;
                div.innerHTML += str ;
                i++ ;
                document.getElementById("num").value = i + "" ;//没添加一次,将总数累加
            }         
        </script>
    </head>
    <body>
        <div id="inputDiv">
        <form name="form1" action="b.jsp">
             <input type="text" id="id10" name="name10"/>&nbsp;
             <input type="text" id="id20" name="name20"/>
<!-- 注意起的名称,name10 name20 后面的“0”是累加用的,前面部分是区分这两个文本域  -->
        </form>
        </div>
        <br>
        <input type="button" onclick="addInput()" value="+" >&nbsp;
        <input type="submit" value="submit" >
        <input type="hidden" id="num" name="num" value="1"><!-- 记录添加次数 -->
    </body>
</html>