使用jquery从动态html表中删除一行
我已将视图中的一些数据保存到数据库中,并在我的 asp.net mvc 应用程序中使用 jquery 将其添加到动态创建的 html 表中。下面给出了脚本。
$(document).ready(function() {
var attributetable = [];
$("#AddAttrib").click(function(event) {
event.preventDefault();
$("#AttributeList").show();
var attribute = $("#Attribute").val();
var attributename = $("#Attribute option:selected").text();
var documentId = $("#DocumentId").val();
var instructionId = $("#InstructionId").val();
var attributevalue = $("#text1").val();
attributetable.push({
attribute: attribute,
attributevalue: attributevalue
});
var row = $("<tr></tr>");
var contents = '<tr><td>' + attribute + '</td><td>' + attributename + '</td><td>' + attributevalue + '</td><td><a id="delete" href="#">Delete</a></td></tr>';
$("#AttributeList").append(contents);
var jsonToPost = {};
jsonToPost.attribute = attribute;
jsonToPost.attributename = attributename;
jsonToPost.attributevalue = attributevalue;
jsonToPost.documentId = documentId;
jsonToPost.instructionId = instructionId;
jsonToPostString = JSON.stringify(jsonToPost);
alert(jsonToPostString);
$.post("/Instruction/CreateInstnAttribute", { data: jsonToPostString });
});
单击“删除”时我需要删除该行。要求是在单击删除链接时删除该行。我怎样才能实现它? 感谢您之前的所有帮助。
I have saved few data from the view to database and added it to dynamically created html table using jquery in my asp.net mvc application. The script is given below.
$(document).ready(function() {
var attributetable = [];
$("#AddAttrib").click(function(event) {
event.preventDefault();
$("#AttributeList").show();
var attribute = $("#Attribute").val();
var attributename = $("#Attribute option:selected").text();
var documentId = $("#DocumentId").val();
var instructionId = $("#InstructionId").val();
var attributevalue = $("#text1").val();
attributetable.push({
attribute: attribute,
attributevalue: attributevalue
});
var row = $("<tr></tr>");
var contents = '<tr><td>' + attribute + '</td><td>' + attributename + '</td><td>' + attributevalue + '</td><td><a id="delete" href="#">Delete</a></td></tr>';
$("#AttributeList").append(contents);
var jsonToPost = {};
jsonToPost.attribute = attribute;
jsonToPost.attributename = attributename;
jsonToPost.attributevalue = attributevalue;
jsonToPost.documentId = documentId;
jsonToPost.instructionId = instructionId;
jsonToPostString = JSON.stringify(jsonToPost);
alert(jsonToPostString);
$.post("/Instruction/CreateInstnAttribute", { data: jsonToPostString });
});
I need to delete the row when "delete" is clicked. The requirement is to delete the row on clicking the delete link. how can i achieve it?
Thanks for all helps in prior.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来好像您正在将
"delete"
ID 用于多行。那是无效的。 ID 必须是唯一的。它应该是一个类。然后,如果
AttributeList
是您的,您可以放置 其上的
.delegate()
处理程序,用于处理对具有"delete" 类的
。元素的点击
将其放入您的
$(document).ready()
调用中。这个 使用
.closest()
来获取最接近的< tr>
祖先,然后.remove()
删除行。它还使用
e.preventDefault()
来禁用默认行为元素。
It seems as though you are using the
"delete"
ID for more than one row. That is invalid. IDs must be unique. It should be a class instead.Then if
AttributeList
is your<table>
, you could place a.delegate()
handler on it to handle clicks on the<a>
elements that have the class"delete"
.Place this inside your
$(document).ready()
call.This uses
.closest()
to get its nearest<tr>
ancestor, then.remove()
to remove the row.It also uses
e.preventDefault()
to disable the default behavior of the<a>
element.