访问.aspx页面服务器端的每个输入字段的值

时间:2012-11-28 13:27:43

标签: jquery asp.net

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

<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页面中每个输入字段的值。

我不知道如何去做..?

以下是它的外观:

add row

1 个答案:

答案 0 :(得分:0)

根据您列出的内容,您的行是动态制作的。你必须聪明才能抓住所有价值观。

Bellow,我将列出字符串数组中的每一行: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 />");
            }
        }
    }
     %>

如果你不理解,请问我更多问题。

Nwafor