1

我只是使用下面的脚本添加新行:

<script>
    $(document).ready(function($) {
        // trigger event when button is clicked
        $("button").click(function() {
            // add new row to table using addTableRow function
            addTableRow($("table"));
            // prevent button redirecting to new page
            return false;
        });

        // function to add a new row to a table by cloning the last row and 
        // incrementing the name and id values by 1 to make them unique
        function addTableRow(table) {
            // clone the last row in the table
            var $tr = $(table).find("tbody tr:last").clone();
            // get the name attribute for the input and select fields
            $tr.find("input,textarea").attr("name", function() {
                // break the field name and it's number into two parts
                var parts = this.id.match(/(\D+)(\d+)$/);
                // create a unique name for the new field by incrementing
                // the number for the previous field by 1
                return 'ctl00_ContentPlaceHolder1' + parts[1] + ++parts[2];
                // repeat for id attributes
            }).attr("id", function() {
                var parts = this.id.match(/(\D+)(\d+)$/);
                return 'ctl00_ContentPlaceHolder1' + parts[1] + ++parts[2];
            });
            // append the new row to the table
            $(table).find("tbody tr:last").after($tr);
        };
    });
</script>

现在我想访问.aspx页面中每个输入字段的值。

我不知道该怎么做..?

这是它的外观:

添加行

4

1 回答 1

0

根据您列出的内容,您的行是动态生成的。你必须很聪明才能捕捉到所有的价值观。

下面,我将列出字符串数组中的每一行:row1[]、row2[]。请使用您的 JavaScript 代码使您的输入名称如下所示:

<table style="width: 100%;">
    <tr>
        <td>
            <input name="row1[]" type="text" /></td>
        <td><input name="row1[]" type="text" /></td>
        <td>
            <select name="row1[]">
                <option></option>
                <option>val 11</option>
                <option>val 12</option>
            </select></td>
    </tr>
    <tr>
        <td>
            <input  name="row2[]" type="text" /></td>
        <td><input  name="row2[]" type="text" /></td>
        <td>
            <select  name="row2[]">
                <option></option>
                <option>val 21</option>
                <option>val 22</option>
            </select></td>
    </tr>
    <tr>
        <td>
            <input id="Submit1" type="submit" value="submit" /></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
</table>

使用下面的代码在服务器端捕获它:

 <%
    if(IsPostBack)
    {
        //pass the form collection object to a variable for easy access
        var r = Request.Form;

        //loop through the form name array
        foreach (var i in r.AllKeys)
        {
            //check if the key starts with row
            if(i.StartsWith("row"))
            {
                //convert it into string array
                string[] sa = r[i].Split(',');

                //use it as you like
                Response.Write(sa[0] + " " + sa[1] + " " + sa[2] +"<br />");
            }
        }
    }
     %>

如果您不明白,请再问我一些问题。

恩瓦福

于 2012-11-28T14:24:01.817 回答