0

如果联系人有多个联系人不同的电话和电子邮件,我有表格来收集联系人信息。用户可以添加更多行以向客户添加更多联系人。我使用 JavaScript 添加新行和 html 控件我的问题是当用户有不止一行时。我不知道如何在某个时候插入超过 1 个联系人以及如何从 Javascript 添加的行中的 html 控件中获取数据

HTML 标签

<asp:Panel ID="p_contacts" runat="server" GroupingText="Contacts">
    <table id="tableContact">
        <thead>
            <tr>
                <th scope="col">
                    Contact
                </th>
                <th scope="col">
                    Contact Type
                </th>
                <th scope="col">
                    Status
                </th>
                <th scope="col">
                    Note
                </th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <input name="contact1" id="contact1" runat="server" onclick="return contact1_onclick()">
                </td>
                <td>
                    <select name="contactType1" id="contactType1" runat="server">
                        <option value="">Please select</option>
                        <option value="1">Email</option>
                        <option value="2">Phone</option>
                    </select>
                </td>
                <td>
                    <select name="status1" id="status1" runat="server">
                        <option value="">Please select</option>
                        <option value="1">Active</option>
                        <option value="2">Not Active</option>
                    </select>
                </td>
                <td>
                    <input name="noteContact1" id="noteContact1" runat="server">
                </td>
            </tr>
        </tbody>
    </table>
    <button id="AddContact">
        <img src="images/button-add_blue.png" alt="btnAdd" height="26" width="26" /></button>
</asp:Panel>

Javascript

$("#AddContact").click(function () {
    // add new row to table using addTableRow function
    addTableRow($("#tableContact"));

    // 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,select").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 parts[1] + ++parts[2];
        // repeat for id attributes
    }).attr("id", function () {
        var parts = this.id.match(/(\D+)(\d+)$/);
        return parts[1] + ++parts[2];
    });
    // append the new row to the table
    $(".date").datepicker();
    $(table).find("tbody tr:last").after($tr);

};

C#

protected void btn_add_Click(object sender, EventArgs e)
    {

        try
        {
            //customer contact inforation
            Contacts contact = new Contacts();
            contact.CustomerID  = 1;
            contact.ContactDetail=  contact1.Value.ToString();
            contact.LabelContactTypeID = (int)contactType1.SelectedIndex;
            contact.Status = Convert.ToBoolean(status1.SelectedIndex);
            contact.Note = noteContact1.Value.ToString();
            //bool successedAddCustomer = Customer.AddNewCustoemr(cust);
            bool successedAddCustomer_Contacts = Contacts.AddNewCustomer_Contact(contact);
            if (!successedAddCustomer_Contacts)
            {

                Response.Write("Customer add");
            }

            else
                Response.Write("Can't add new customer");

        }

        catch
        { }

    }
4

1 回答 1

0

添加一个新的隐藏字段,该字段将包含添加项目的计数。在后面的代码中,使用 Request.Form 集合在循环中查找项目,例如

for (int i = 0, i < Request.form["HiddenFieldHere"], i++)
{
        Contacts contact = new Contacts(); 
        contact.CustomerID  = i; 
        contact.ContactDetail=  Request.Form[string.format("contact{0},i)].ToString(); 
}

这个例子并不完美,但我希望它能给你一些指导。

于 2012-10-16T17:24:21.223 回答