在ASP.NET中使用jQuery动态添加/删除表行时出现问题
Issue in adding/removing table rows dynamically in ASP .NET using jQuery
我有一个用户控件,允许用户输入多个电子邮件收件人。在页面加载时,我有一个表行,其中包含名称和电子邮件地址文本框(没有删除按钮(
所有其他表行都是使用jQuery添加的,因此添加的每一行都有一个Remove按钮,允许用户删除特定的行。以下是我的代码:
<div>
<table cellspacing="5" runat="server" id="tblEmailRecipients" clientidmode="Static">
<tr>
<td colspan="4">Custom Email Recipients</td>
</tr>
<tr>
<td colspan="3">
<input type="button" value="Add More" onclick="AddNewTableRow();" id="btnAdd" />
</td>
</tr>
<tr style="font-weight:bold">
<td>Name</td>
<td>Email Address</td>
<td></td>
</tr>
<tr>
<td><asp:TextBox runat="server" ID="txtName" ></asp:TextBox> </td>
<td><asp:TextBox runat="server" ID="txtEmailAddress" ></asp:TextBox>
<asp:RegularExpressionValidator ID="regexValidatorEmailAddress" runat="server" ErrorMessage="Invalid Email Address" ControlToValidate="txtEmailAddress"
Display="Dynamic" ValidationExpression="'w+([-+.']'w+)*@'w+([-.]'w+)*'.'w+([-.]'w+)*"></asp:RegularExpressionValidator>
</td>
<td>
<%--<input type="button" value="Remove" onclick="DeleteTableRow(this);" id="btnRemove" name="btnRemove" />--%>
</td>
</tr>
</table>
</div>
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click"
style="height: 26px" />
jQuery:
function DeleteTableRow(ctrl) {
//delete control table row
$($(ctrl).closest("tr")).remove();
}
function AddNewTableRow() {
//Get Row clone and add as last row
//Also, created row clone but with different control ids
var i = $("#tblEmailRecipients tr").length;
var newRow = $("#tblEmailRecipients tr:last").clone().find("input").each(
function () {
$(this).attr({
'id': function (_, id) { return id + i },
'name': function (_, name) { return name + i },
});
}).end();
$("#tblEmailRecipients tr:last").after(newRow);
$("#tblEmailRecipients tr:last input:text").val("");
//Add 'Remove' button in the last column - this needs to be done only for second row, all rows after 2nd row will auto. contain Remove button as they are cloned from prev. row
var lastCell = $("#tblEmailRecipients tr:last td:last");
var lastCellContents = jQuery.trim($(lastCell).html());
if (lastCellContents == '') {
var btnRemoveName = "btnRemove" + i;
var btnRemove = "<input type='button' value='Remove' onclick='DeleteTableRow(this);' id='" + btnRemoveName + "' name='" + btnRemoveName + "' />";
$(btnRemove).appendTo($(lastCell));
}
//Move focus to newly added row Textbox
$("#tblEmailRecipients tr:last input:first").focus();
}
我面临的问题是在处理btnSave_Click时,
即使我添加了2个或多个收件人(表行(,在后面的代码中,我只看到包含电子邮件收件人的第一个表行。
我想知道为什么我不能访问在运行时添加的其他表行。。。它们在回发后也会在UI上丢失?
请你带路好吗。
谢谢!
尝试为每行中的编辑框提供动态id,这意味着在每行中为所有编辑框提供相同的id。每行的id应该不同。
相关文章:
- 关于使用Animate.css向和项添加和删除动画类的问题
- 删除时出现Javascript链表问题
- Small Javascript从动态表单中删除多个元素的问题
- 使用javascript删除元素时出现的问题
- Javascript/JQuery删除代码中的父TR问题
- jQuery使用.of()删除事件处理程序时出现的问题
- 骨干.js - 从推文中删除模型时出现奇怪的问题
- 使用 Javascript 动态删除行.我的代码有什么问题
- 使用javascript从字符串中删除额外或特殊字符时遇到问题
- NetSuite行项目删除内部循环问题
- 使用jquery从URL中删除查询字符串(处理问题)
- 使用数据库中的图像填充dropzone.js删除图像的问题
- 正在删除单击问题上的活动类
- 使用jquery删除和获取html元素时遇到问题
- 问题在于克隆、拼接、删除、追加 jquery 函数
- 拉斐尔的问题和删除 svg 中的对象
- 从数组及其父数组中删除拼接 - 奇怪的问题
- HTML5拖放;Internet Explorer中的删除问题(无法访问dataTransfer属性)
- Jquery UI对话框删除问题
- 用jquery和复选框在数组中添加和删除问题