我想删除任意一行的id
I want to remove any row by its id
var numall = 2;
$('.add_row').click(function(){
var displaytext = '';
displaytext += ' <tr id="tr'+numall+'" ><td>';
displaytext +='<input type="text" name="length[]" class="focuson selector remove_rows" onkeyup="func2()" size="20"/>';
displaytext +='</td><td>';
displaytext +='<input type="text" name="width[]" class="selector remove_rows" onkeyup="func2()" size="5"/>';
displaytext +='</td><td>';
displaytext +='<input type="text" name="weight[]" class="selector remove_rows" onkeyup="func2()" size="15"/>';
displaytext +='</td><td>';
displaytext +='<input type="text" name="rate[]" class="selector remove_rows" onkeyup="func2()" size="15"/>';
displaytext +='</td><td>';
displaytext +='<input type="text" name="qty[]" id="dis[]" class="selector remove_rows" onkeyup="func2()" size="15"/>';
displaytext +='</td><td>';
displaytext +='<input type="text" name="total[]" id="total[]" class="remove_rows" size="20"/> ';displaytext +='</td><td>';
displaytext +='<input type="button" id="' + numall + '" value="remove"/>';
displaytext +='</td></tr>';
$('#myTable tbody').append(displaytext);
numall++;
$('.focuson').focus();
$(".selector").attr("required", true);
$("#"+numall).click(function(){
numall--;
var buttonId = $(this).attr("id");
//write the logic for removing from the array
$("#tr"+ numall).remove();
});
});
您需要使用.on
API来调用动态创建的元素的函数。
$("table").on("click", "tr", function() {
numall--;
$(this).remove();
});
根本不需要id和numall变量。给remove button一个类,然后找到父tr,然后删除它
$('.add_row').click(function(){
var displaytext = '';
displaytext += ' <tr ><td>';
displaytext +='<input type="text" name="length[]" class="focuson selector remove_rows" onkeyup="func2()" size="20"/>';
displaytext +='</td><td>';
displaytext +='<input type="text" name="width[]" class="selector remove_rows" onkeyup="func2()" size="5"/>';
displaytext +='</td><td>';
displaytext +='<input type="text" name="weight[]" class="selector remove_rows" onkeyup="func2()" size="15"/>';
displaytext +='</td><td>';
displaytext +='<input type="text" name="rate[]" class="selector remove_rows" onkeyup="func2()" size="15"/>';
displaytext +='</td><td>';
displaytext +='<input type="text" name="qty[]" id="dis[]" class="selector remove_rows" onkeyup="func2()" size="15"/>';
displaytext +='</td><td>';
displaytext +='<input type="text" name="total[]" id="total[]" class="remove_rows" size="20"/> ';displaytext +='</td><td>';
displaytext +='<input type="button" value="remove" class="remove"/>';
displaytext +='</td></tr>';
$('#myTable tbody').append(displaytext);
$('.focuson').focus();
$(".selector").attr("required", true);
$('.remove').on( "click", function() {
$(this).parent().parent().remove();
});
});
演示:http://jsfiddle.net/afna6xnq/2/
相关文章:
- 希望日期开始结束于while循环中的一个房间id的一个数组
- 有没有一种简单的方法可以用包含id和名称(从a到Z)的对象填充数组
- 数据互绑定问题:转换器只运行一次,无法绑定元素的 ID
- 如何获取一个人的Google+ ID
- 如何附加一个id's的详细信息只通过php或jquery提供一次
- 如何引用表单元格中任意给定行的一组复选框
- 如何在一个表的输入中搜索一组特定的值,而不是另一个表's id(s)值以添加css
- JQGridPOST一列's值而不是ID
- JavaScript-更改一组包含特定文本和变量ID元素的链接
- 用angularjs将id从一页传递到另一页
- 如何在一次点击中获取所有选定的选项值ID
- 如何从一组元素中获取 ID 并保存在数组中
- 学习JavaScript:有没有办法在不使用个人ID的情况下更改一组图像的图像文件
- 有没有一种通用方法可以通过Javascript访问没有id的DOM元素
- 按另一个数组的ID从一个数组获取数据
- 在jquery中选择ID+任意数字
- 更改html id是一种好的做法吗
- 如何通过任意一组键对数组元素进行分组
- 按下任意一张卡片,使p标签进入输入字段
- AngularJs:通过2列中的任意一列进行编程过滤