支持所有浏览器,通过JS为 Table表增加、删除一行
2015-03-18 14:03
405 查看
function addTableRow(tableId) {
var row = $("#" + tableId + " tr:eq(1)").clone();
var rowIndex = $("#" + tableId + " tr").length;
//遍历TD
row.find('td').each(function (i) {
var colIndex = i + 1;
$(this).find('input').each(function (j) {
//绑定验证规则
if (typeof ($(this).attr('ruleValue')) != "undefined" && $(this).attr("IsChecking") == '1') {
$(this).attr('data-rule', $(this).attr('ruleValue'));
}
//单选框、复选框
if ($(this).attr('type') == 'radio' || $(this).attr('type') == 'checkbox') {
$(this).attr("checked", false);
var html = $(this).prop('outerHTML');
html = html.replace($(this).attr('name'), $(this).attr('name') + rowIndex + colIndex);
$(this).prop('outerHTML', html);
}
else {
$(this).attr('value', '');
$(this).val('');
$(this).attr('id', $(this).attr('id') + rowIndex + colIndex);
$(this).attr('name', $(this).attr('id'));
}
});
//文本框
$(this).find('textarea').each(function (i) {
//绑定验证规则
if (typeof ($(this).attr('ruleValue')) != "undefined" && $(this).attr("IsChecking") == '1') {
$(this).attr('data-rule', $(this).attr('ruleValue'));
}
$(this).attr('value', '');
$(this).val('');
$(this).attr('id', $(this).attr('id') + rowIndex + colIndex);
$(this).attr('name', $(this).attr('id'));
});
//下拉框
$(this).find('select').each(function (i) {
//绑定验证规则
if (typeof ($(this).attr('ruleValue')) != "undefined" && $(this).attr("IsChecking") == '1') {
$(this).attr('data-rule', $(this).attr('ruleValue'));
}
$(this).attr('value', '');
$(this).attr('id', $(this).attr('id') + rowIndex + colIndex);
$(this).attr('name', $(this).attr('id'));
});
//移除无效元素
$(this).find('span').each(function (i) {
if (typeof ($(this).attr('for')) != "undefined") {
$(this).remove();
}
});
});
$('#' + tableId).append(row);
}
//给table增加一行
function addTableRow_IE(tableId) {
var t = document.getElementById(tableId);
var cloneTd = t.firstChild.childNodes[1].cloneNode(true);
//遍历TR
var i = t.firstChild.childNodes.length;
//遍历TD
var tdList = cloneTd.childNodes;
for (var j = 0; j < tdList.length; j++) {
var inputList = tdList[j].getElementsByTagName("input");
var textarea = tdList[j].getElementsByTagName("textarea");
var selectOption = tdList[j].getElementsByTagName("select");
for (var k = 0; k < inputList.length; k++) {
var obj = inputList[k];
if (obj.getAttribute("ruleValue") != null && obj.getAttribute("IsChecking") == '1') {
obj.setAttribute('data-rule', obj.getAttribute('ruleValue'));
}
if (obj.getAttribute('type') == 'radio' || obj.getAttribute('type') == 'checkbox') {
obj.checked = false;
var html = obj.outerHTML;
html = html.replace($(obj).attr('name'), $(obj).attr('name') + i + j);
obj.outerHTML = html;
}
else {
obj.value = '';
obj.id = obj.id + i + j;
$(obj).attr('name', obj.id);
}
}
for (var k = 0; k < textarea.length; k++) {
var obj = textarea[k];
if (obj.getAttribute("ruleValue") != null && obj.getAttribute("IsChecking") == '1') {
obj.setAttribute('data-rule', obj.getAttribute('ruleValue'));
}
obj.value = '';
obj.id = obj.id + i + j;
$(obj).attr('name', obj.id);
}
for (var k = 0; k < selectOption.length; k++) {
var obj = selectOption[k];
if (obj.getAttribute("ruleValue") != null && obj.getAttribute("IsChecking") == '1') {
obj.setAttribute('data-rule', obj.getAttribute('ruleValue'));
}
obj.id = obj.id + i + j;
$(obj).attr('name', obj.id);
obj.value = '';
}
//遍历TD里面多个控件
var spanList = tdList[j].getElementsByTagName("span");
for (var k = 0; k < spanList.length; k++) {
var obj = spanList[k];
if (obj.getAttribute('for') != null) { // == trList[1].childNodes[j].firstChild.id) {
obj.parentNode.removeChild(obj);
}
}
}
t.firstChild.appendChild(cloneTd);
}
//通过当前行后面的delete链接删除本行
function delTableRow(tableId, obj) {
var IsReadonly = obj.getAttribute("IsReadonly");
//只读-不可编辑
if (IsReadonly == 1) {
return;
}
var t = document.getElementById(tableId);
if (t.rows.length <= 2) {
alert("明细表单至少显示一行!");
}
else {
t.deleteRow(obj.parentNode.parentNode.rowIndex);
}
}
var row = $("#" + tableId + " tr:eq(1)").clone();
var rowIndex = $("#" + tableId + " tr").length;
//遍历TD
row.find('td').each(function (i) {
var colIndex = i + 1;
$(this).find('input').each(function (j) {
//绑定验证规则
if (typeof ($(this).attr('ruleValue')) != "undefined" && $(this).attr("IsChecking") == '1') {
$(this).attr('data-rule', $(this).attr('ruleValue'));
}
//单选框、复选框
if ($(this).attr('type') == 'radio' || $(this).attr('type') == 'checkbox') {
$(this).attr("checked", false);
var html = $(this).prop('outerHTML');
html = html.replace($(this).attr('name'), $(this).attr('name') + rowIndex + colIndex);
$(this).prop('outerHTML', html);
}
else {
$(this).attr('value', '');
$(this).val('');
$(this).attr('id', $(this).attr('id') + rowIndex + colIndex);
$(this).attr('name', $(this).attr('id'));
}
});
//文本框
$(this).find('textarea').each(function (i) {
//绑定验证规则
if (typeof ($(this).attr('ruleValue')) != "undefined" && $(this).attr("IsChecking") == '1') {
$(this).attr('data-rule', $(this).attr('ruleValue'));
}
$(this).attr('value', '');
$(this).val('');
$(this).attr('id', $(this).attr('id') + rowIndex + colIndex);
$(this).attr('name', $(this).attr('id'));
});
//下拉框
$(this).find('select').each(function (i) {
//绑定验证规则
if (typeof ($(this).attr('ruleValue')) != "undefined" && $(this).attr("IsChecking") == '1') {
$(this).attr('data-rule', $(this).attr('ruleValue'));
}
$(this).attr('value', '');
$(this).attr('id', $(this).attr('id') + rowIndex + colIndex);
$(this).attr('name', $(this).attr('id'));
});
//移除无效元素
$(this).find('span').each(function (i) {
if (typeof ($(this).attr('for')) != "undefined") {
$(this).remove();
}
});
});
$('#' + tableId).append(row);
}
//给table增加一行
function addTableRow_IE(tableId) {
var t = document.getElementById(tableId);
var cloneTd = t.firstChild.childNodes[1].cloneNode(true);
//遍历TR
var i = t.firstChild.childNodes.length;
//遍历TD
var tdList = cloneTd.childNodes;
for (var j = 0; j < tdList.length; j++) {
var inputList = tdList[j].getElementsByTagName("input");
var textarea = tdList[j].getElementsByTagName("textarea");
var selectOption = tdList[j].getElementsByTagName("select");
for (var k = 0; k < inputList.length; k++) {
var obj = inputList[k];
if (obj.getAttribute("ruleValue") != null && obj.getAttribute("IsChecking") == '1') {
obj.setAttribute('data-rule', obj.getAttribute('ruleValue'));
}
if (obj.getAttribute('type') == 'radio' || obj.getAttribute('type') == 'checkbox') {
obj.checked = false;
var html = obj.outerHTML;
html = html.replace($(obj).attr('name'), $(obj).attr('name') + i + j);
obj.outerHTML = html;
}
else {
obj.value = '';
obj.id = obj.id + i + j;
$(obj).attr('name', obj.id);
}
}
for (var k = 0; k < textarea.length; k++) {
var obj = textarea[k];
if (obj.getAttribute("ruleValue") != null && obj.getAttribute("IsChecking") == '1') {
obj.setAttribute('data-rule', obj.getAttribute('ruleValue'));
}
obj.value = '';
obj.id = obj.id + i + j;
$(obj).attr('name', obj.id);
}
for (var k = 0; k < selectOption.length; k++) {
var obj = selectOption[k];
if (obj.getAttribute("ruleValue") != null && obj.getAttribute("IsChecking") == '1') {
obj.setAttribute('data-rule', obj.getAttribute('ruleValue'));
}
obj.id = obj.id + i + j;
$(obj).attr('name', obj.id);
obj.value = '';
}
//遍历TD里面多个控件
var spanList = tdList[j].getElementsByTagName("span");
for (var k = 0; k < spanList.length; k++) {
var obj = spanList[k];
if (obj.getAttribute('for') != null) { // == trList[1].childNodes[j].firstChild.id) {
obj.parentNode.removeChild(obj);
}
}
}
t.firstChild.appendChild(cloneTd);
}
//通过当前行后面的delete链接删除本行
function delTableRow(tableId, obj) {
var IsReadonly = obj.getAttribute("IsReadonly");
//只读-不可编辑
if (IsReadonly == 1) {
return;
}
var t = document.getElementById(tableId);
if (t.rows.length <= 2) {
alert("明细表单至少显示一行!");
}
else {
t.deleteRow(obj.parentNode.parentNode.rowIndex);
}
}
相关文章推荐
- 通过JS为 Table表增加一行,通过复选框删除多行,也可通过所在行的delete链接删除本行
- JS :点击按钮table增加一行,删除一行
- js----[转]通过js动态清空、增加、删除、修改下拉框中的元素
- 利用js实现table增加一行
- 动态表格,通过点击按钮增加或者删除一行表格!
- 为 SELECT 对象增加或删除选项的方法在各浏览器中的支持情况不同
- 利用js实现table增加一行
- 删除table中的一行js实现
- js中删除table里所有行
- JS 删除Table中的一行
- js增加删除table信息
- JS banner 切换,幻灯片,支持所有浏览器
- 常用的js动态增加(删除)table数据行的功能
- JS banner 切换,幻灯片,支持所有浏览器
- 使用jquery操作table元素:增加或删除一行
- javascript开发系列(table操作,table增加一行,删除一行,取行号,列号)
- Js选中checkbox后获取table内一行TD所有数据
- 复制Input内容的js代码_支持所有浏览器,修正了Firefox3.5以上的问题
- 中文前端UI框架Kit(九)IE下实现W3C标准的range对象所有api,支持通过xpath跨浏览器实现特定选区高亮,书签标记功能
- js 动态增加和删除 table 行 .