Javascript 从下拉列表中动态选择文本输入的数量

Javascript 从下拉列表中动态选择文本输入的数量,javascript,jquery,html,Javascript,Jquery,Html,我试图根据下拉列表中的选择来选择文本输入的数量。例如,如果用户在下拉列表中选择数字4,则将有4x4个文本输入。文本输入中的值将映射到等效的2d数组中。我可以保存到2d数组中,但不能动态生成输入 var array = new Array(); function insert(val){ array[[0],[array.length]]=val; } function insert2(val){

我试图根据下拉列表中的选择来选择文本输入的数量。例如,如果用户在下拉列表中选择数字4,则将有4x4个文本输入。文本输入中的值将映射到等效的2d数组中。我可以保存到2d数组中,但不能动态生成输入

        var array = new Array();

        function insert(val){
            array[[0],[array.length]]=val;
        }

        function insert2(val){
            array[[1],[array.length]]=val;
        }

        function show() {
            var string="<b>All Element of the Array :</b><br>";
            for(i = 0; i < array.length; i++) {
                string =string+array[i][0]+"<br>";
                string =string+array[0][i]+"<br>";
            }
            if(array.length > 0)
                document.getElementById('myDiv').innerHTML = string;
        }
    </script>

</head>

<body>
    <h2>JavaScript Array from Input</h2>
    <form name="form1">
        <table width="407">
            <tr>
                <td width="154" align="right"><b>Name</b>
                <td width="9"><b>&nbsp;:</b>
                <td width="224">
                <input type="integer" name="name"/>
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">


                <td width="154" align="right"><b>Name2</b>
                <td width="9"><b>&nbsp;:</b>
                <td width="224">
                <input type="integer" name="name2"/>
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">
                <input type="button" Value="Add Into Array" 
                       onclick="insert(this.form.name.value), insert2(this.form.name2.value);"/>

                        <input type="button" Value="alert" onclick="alert(array[1][0])"/>
            </tr>
        </table>
我现在得到的是,我可以在按下按钮时创建一行字段

            <script 
        type="text/javascript" 
        src="http://cachefile.net/scripts/jquery/1.2.3/jquery-1.2.3.min.js">
    </script>
    <script type="text/javascript">
    $(function(){
        // start a counter for new row IDs
        // by setting it to the number
        // of existing rows
        var newRowNum = 0;

        // bind a click event to the "Add" link
        $('#addnew').click(function(){
            // increment the counter
            newRowNum += 1;

            // get the entire "Add" row --
            // "this" refers to the clicked element
            // and "parent" moves the selection up
            // to the parent node in the DOM
            var addRow = $(this).parent().parent();

            // copy the entire row from the DOM
            // with "clone"
            var newRow = addRow.clone();

            // set the values of the inputs
            // in the "Add" row to empty strings
            $('input', addRow).val('');

            // replace the HTML for the "Add" link 
            // with the new row number
            $('td:first-child', newRow).html(newRowNum);

            // insert a remove link in the last cell
            $('td:last-child', newRow).html('<a href="" class="remove">Remove<\/a>');

            // loop through the inputs in the new row
            // and update the ID and name attributes
            $('input', newRow).each(function(i){
                var newID = newRowNum + '_' + i;
                $(this).attr('id',newID).attr('name',newID);
            });

            // insert the new row into the table
            // "before" the Add row
            addRow.before(newRow);

            // add the remove function to the new row
            $('a.remove', newRow).click(function(){
                $(this).parent().parent().remove();
                return false;               
            });

            // prevent the default click
            return false;
        });
    });

    </script>
</head>
<body>
    <form>
        <table id="tabdata">
            <thead>
                <tr>
                    <th>Row</th>
                    <th>Cell 1</th>
                    <th>Cell 2</th>
                    <th>Cell 3</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><a id="addnew" href="">Add</a></td>
                    <td><input id="n0_1" name="n0_1" type="text" /></td>
                    <td><input id="n0_2" name="n0_2" type="text" /></td>
                    <td><input id="n0_3" name="n0_3" type="text" /></td>
                    <td></td>
                </tr>
            </tbody>
        </table>

        <input id="go" name="go" type="submit" value=" Save " />
    </form>
        var array = new Array();

        function insert(val){
            array[[0],[array.length]]=val;
        }

        function insert2(val){
            array[[1],[array.length]]=val;
        }

        function show() {
            var string="<b>All Element of the Array :</b><br>";
            for(i = 0; i < array.length; i++) {
                string =string+array[i][0]+"<br>";
                string =string+array[0][i]+"<br>";
            }
            if(array.length > 0)
                document.getElementById('myDiv').innerHTML = string;
        }
    </script>

</head>

<body>
    <h2>JavaScript Array from Input</h2>
    <form name="form1">
        <table width="407">
            <tr>
                <td width="154" align="right"><b>Name</b>
                <td width="9"><b>&nbsp;:</b>
                <td width="224">
                <input type="integer" name="name"/>
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">


                <td width="154" align="right"><b>Name2</b>
                <td width="9"><b>&nbsp;:</b>
                <td width="224">
                <input type="integer" name="name2"/>
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">
                <input type="button" Value="Add Into Array" 
                       onclick="insert(this.form.name.value), insert2(this.form.name2.value);"/>

                        <input type="button" Value="alert" onclick="alert(array[1][0])"/>
            </tr>
        </table>
我创建的用于保存到数组中的脚本是

        var array = new Array();

        function insert(val){
            array[[0],[array.length]]=val;
        }

        function insert2(val){
            array[[1],[array.length]]=val;
        }

        function show() {
            var string="<b>All Element of the Array :</b><br>";
            for(i = 0; i < array.length; i++) {
                string =string+array[i][0]+"<br>";
                string =string+array[0][i]+"<br>";
            }
            if(array.length > 0)
                document.getElementById('myDiv').innerHTML = string;
        }
    </script>

</head>

<body>
    <h2>JavaScript Array from Input</h2>
    <form name="form1">
        <table width="407">
            <tr>
                <td width="154" align="right"><b>Name</b>
                <td width="9"><b>&nbsp;:</b>
                <td width="224">
                <input type="integer" name="name"/>
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">


                <td width="154" align="right"><b>Name2</b>
                <td width="9"><b>&nbsp;:</b>
                <td width="224">
                <input type="integer" name="name2"/>
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">
                <input type="button" Value="Add Into Array" 
                       onclick="insert(this.form.name.value), insert2(this.form.name2.value);"/>

                        <input type="button" Value="alert" onclick="alert(array[1][0])"/>
            </tr>
        </table>

我还没有使这两个脚本兼容。一次解决一个问题。

jsFiddle显示这两个脚本在页面上下文中的实际功能将非常有用。如果在名称和名称2中添加值。单击添加,然后单击警报,它将显示数组[1][0]中的值,这表明我可以添加到二维数组中。下一部分显示我可以添加文本输入行,但我不知道如何使用下拉列表选择行数和列数,然后添加到数组中。
        var array = new Array();

        function insert(val){
            array[[0],[array.length]]=val;
        }

        function insert2(val){
            array[[1],[array.length]]=val;
        }

        function show() {
            var string="<b>All Element of the Array :</b><br>";
            for(i = 0; i < array.length; i++) {
                string =string+array[i][0]+"<br>";
                string =string+array[0][i]+"<br>";
            }
            if(array.length > 0)
                document.getElementById('myDiv').innerHTML = string;
        }
    </script>

</head>

<body>
    <h2>JavaScript Array from Input</h2>
    <form name="form1">
        <table width="407">
            <tr>
                <td width="154" align="right"><b>Name</b>
                <td width="9"><b>&nbsp;:</b>
                <td width="224">
                <input type="integer" name="name"/>
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">


                <td width="154" align="right"><b>Name2</b>
                <td width="9"><b>&nbsp;:</b>
                <td width="224">
                <input type="integer" name="name2"/>
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">
            </tr>
            <tr>
                <td width="154" align="right">
                <td width="9">
                <td width="224">
                <input type="button" Value="Add Into Array" 
                       onclick="insert(this.form.name.value), insert2(this.form.name2.value);"/>

                        <input type="button" Value="alert" onclick="alert(array[1][0])"/>
            </tr>
        </table>