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

时间:2012-02-24 14:37:10

标签: javascript jquery html

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

我目前得到的是我可以按下按钮创建一行字段。

            <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>

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

0 个答案:

没有答案