博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js table 操作-----实现table的插入、修改、删除
阅读量:6154 次
发布时间:2019-06-21

本文共 5515 字,大约阅读时间需要 18 分钟。

上边是一个表,下边是一些录入控件text ,select

实现了:插入 ,删除,修改,读取,验证类型唯一性(每种类型只能输入一个记录)

<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
HTML
>
<
HEAD
>
<
TITLE
>
 New Document 
</
TITLE
>
<
META 
NAME
="Generator"
 CONTENT
="EditPlus"
>
<
META 
NAME
="Author"
 CONTENT
=""
>
<
META 
NAME
="Keywords"
 CONTENT
=""
>
<
META 
NAME
="Description"
 CONTENT
=""
>
</
HEAD
>
<
script
>
    
var
 flg
=
false
;
    
var
 selectedColor 
=
 
"
#99CCCC
"
;
    
var
 initColor 
=
 
"
#ffff99
"
;
    
var
 selectedRowIndex 
=
 
""
;
    
var
 editObj;
    
function
 add()
{
        
var
 cell;
        
var
 textNode;
        
//
 add head
        
var
 type 
=
 document.getElementsByName(
"
type
"
)[
0
];
        head 
=
 type.options[type.selectedIndex].text;
        
//
check exist
        
var
 hiddenHeads 
=
 document.getElementsByName(
"
hiddenHead
"
);
        
for
(i
=
0
;i
<
hiddenHeads.length;i
++
)
{
            
if
(hiddenHeads[i].value 
==
 type.value)
{
                alert(
"
这个类型的记录已经存在
"
);
                
return
 ;
            }
        }
        
//
add row
        tbl 
=
 document.getElementById(
"
paramTbl
"
);
        rowsLen 
=
 tbl.rows.length;
        row 
=
 tbl.insertRow(rowsLen);        
        
        
//
create head tag
        textNode 
=
 document.createTextNode(head);        
        cell 
=
 row.insertCell(
0
)
        cell.setAttribute(
"
hight
"
,
"
22
"
);
        cell.appendChild(textNode);        
        row.appendChild(cell);        
        
//
add param tag
        
for
(i
=
1
;i
<
4
;i
++
)
{            
            paramValue 
=
 document.getElementsByName(
"
param
"
+
i)[
0
].value;
            textNode 
=
 document.createTextNode(paramValue);            
            cell 
=
 row.insertCell(i)
            cell.setAttribute(
"
hight
"
,
"
22
"
);
            cell.appendChild(textNode);
            row.appendChild(cell);             
        }
        
//
add head value
        hiddenHead 
=
 document.createElement(
"
<input type='hidden' name='hiddenHead' value='
"
+
type.value
+
"
'/>
"
);        
        cell 
=
 row.insertCell(
4
)        
        cell.setAttribute(
"
hight
"
,
"
22
"
);
        cell.appendChild(hiddenHead);
        row.appendChild(cell);
        
//
add param value
        
for
(i
=
5
;i
<
8
;i
++
)
{
            paramValue 
=
 document.getElementsByName(
"
param
"
+
(i
-
4
))[
0
].value;
            hidden 
=
 document.createElement(
"
<input type='hidden' name='hiddenParam
"
+
(i
-
4
)
+
"
' value='
"
+
paramValue
+
"
'/>
"
);
            cell 
=
 row.insertCell(i)
            cell.style.display
=
'none';
            cell.appendChild(hidden);
            row.appendChild(cell);   
        }
        row.onclick
=
function
()
{rowClick(
this
);}
;
        row.bgColor 
=
 initColor;
        init();
    }
    
function
 edit()
{
        tbl 
=
 document.getElementById(
"
paramTbl
"
);
        
if
(selectedRowIndex
==
""
)
{
            alert(
"
please select a row
"
);
            
return
 ;
        }
        
for
(i
=
1
;i
<
4
;i
++
)
{
            paramObj 
=
 document.getElementsByName(
"
param
"
+
i)[
0
];
            hiddenObj 
=
 document.getElementsByName(
"
hiddenParam
"
+
i)[selectedRowIndex
-
1
];
            hiddenObj.value 
=
 paramObj.value;
            editObj.cells[i].innerText
=
paramObj.value;
        }
        init();
    }
    
function
 del()
{
        tbl 
=
 document.getElementById(
"
paramTbl
"
);
        tbl.deleteRow(selectedRowIndex);
        selectedRowIndex 
=
 
""
;
        init();
    }
    
function
 rowClick(obj)
{
        tbl 
=
 document.getElementById(
"
paramTbl
"
);
        
if
(selectedRowIndex 
!=
 
""
)
{
            tbl.rows[selectedRowIndex].bgColor 
=
 initColor;
        }
        selectedRowIndex 
=
 obj.rowIndex;
        obj.bgColor 
=
 selectedColor;
        
//
reset select 
        
var
 type 
=
 document.getElementsByName(
"
type
"
)[
0
];
        
var
 hiddenHead 
=
 document.getElementsByName(
"
hiddenHead
"
)[selectedRowIndex
-
1
];        
        
var
 opts 
=
 type.options;
        
if
(flg)
{
        alert(selectedRowIndex
-
1
);
        alert(hiddenHead);
        alert(hiddenHead.value);
        }
        
for
(i
=
0
;i
<
opts.length;i
++
)
{    
            
if
(opts[i].value 
==
 hiddenHead.value )
{
                opts[i].selected 
=
 
true
;
            }
        }
        
//
copy param value
        
for
(i
=
1
;i
<
4
;i
++
)
{
            paramObj 
=
 document.getElementsByName(
"
param
"
+
i)[
0
];
            hiddenObj 
=
 document.getElementsByName(
"
hiddenParam
"
+
i)[selectedRowIndex
-
1
];            
            paramObj.value 
=
 hiddenObj.value;
            
//
alert(hiddenObj.value);
        }
        editObj
=
obj;
    }
    
function
 init()
{
        
for
(i
=
1
;i
<
4
;i
++
)
{            
            param 
=
 document.getElementsByName(
"
param
"
+
i)[
0
];
            param.value
=
""
;
        }
        
var
 type 
=
 document.getElementsByName(
"
type
"
)[
0
];
        type.options[
0
].selected
=
true
;
    }
    
function
 test()
{
        
        hiddenObjs 
=
 document.getElementsByName(
"
hiddenParam1
"
)
        
for
(i
=
0
;i
<
hiddenObjs.length;i
++
)
{
            alert(hiddenObjs[i].value);
        }
        
var
 hiddenHeads 
=
 document.getElementsByName(
"
hiddenHead
"
);    
        
for
(i
=
0
;i
<
hiddenHeads.length;i
++
)
{
            alert(hiddenHeads[i].value);
        }
    }
    
function
 test2()
{
        
if
(flg)
{
            flg
=
false
;
        }
else
{
            flg
=
true
;
        }
    }
    
function
 reset()
{
        tbl 
=
 document.getElementById(
"
paramTbl
"
);
        
if
(selectedRowIndex
!=
""
)
{
            tbl.rows[selectedRowIndex].bgColor 
=
 initColor;
            selectedRowIndex
=
""
;
        }
        
for
(i
=
1
;i
<
4
;i
++
)
{            
            param 
=
 document.getElementsByName(
"
param
"
+
i)[
0
];
            param.value
=
""
;
        }
    }
</
script
>
<
BODY
>
    
<
div 
style
="height:132px; width:100%; overflow-x:auto; overflow-y:auto; background-color:#ffff99;"
 
>
    
<
table 
id
="paramTbl"
 width
="100%"
>
        
<
tr
>
            
<
td
>
类型
</
td
>
            
<
td
>
参数1
</
td
>
            
<
td
>
参数二
</
td
>
            
<
td
>
参数三
</
td
>
        
</
tr
>
    
</
table
>
    
</
div
>
    
<
table
>
        
<
tr
>
            
<
td
>
                
<
select 
name
="type"
 onchange
="reset()"
>
                    
<
option 
value
="typeA"
>
类型A
</
option
>
                    
<
option 
value
="typeB"
>
类型B
</
option
>
                
</
select
>
            
</
td
>
            
<
td
>
                
<
input 
type
="text"
 name
="param1"
/>
            
</
td
>
            
<
td
>
                
<
input 
type
="text"
 name
="param2"
/>
            
</
td
>
            
<
td
>
                
<
input 
type
="text"
 name
="param3"
/>
            
</
td
>
        
</
tr
>
        
<
tr
>
            
<
td
><
input 
type
="button"
 onclick
="add()"
 value
="add"
/></
td
>
            
<
td
><
input 
type
="button"
 onclick
="edit()"
 value
="edit"
/></
td
>
            
<
td
><
input 
type
="button"
 onclick
="del()"
 value
="del"
/></
td
>
            
<
td
>
<!--
<input type="button" οnclick="test()" value="test"/>
-->
</
td
>
        
</
tr
>
    
</
table
>
    
<!--
<td><input type="button" οnclick="test2()" value="test2"/></td>
<td><input type="button" οnclick="alert(selectedRowIndex)" value="test2"/></td>
-->
</
BODY
>
</
HTML
>
本文转自博客园执着的笨蛋的博客,原文链接:
,如需转载请自行联系原博主。
你可能感兴趣的文章
Windows DHCP Server基于MAC地址过滤客户端请求实现IP地址的分配
查看>>
命令查询每个文件文件数
查看>>
《跟阿铭学Linux》第8章 文档的压缩与打包:课后习题与答案
查看>>
RAC表决磁盘管理和维护
查看>>
Apache通过mod_php5支持PHP
查看>>
发布一个TCP 吞吐性能测试小工具
查看>>
java学习:jdbc连接示例
查看>>
PHP执行批量mysql语句
查看>>
Extjs4.1.x 框架搭建 采用Application动态按需加载MVC各模块
查看>>
Silverlight 如何手动打包xap
查看>>
建筑电气暖通给排水协作流程
查看>>
JavaScript面向对象编程深入分析(2)
查看>>
linux 编码转换
查看>>
POJ-2287 Tian Ji -- The Horse Racing 贪心规则在动态规划中的应用 Or 纯贪心
查看>>
Windows8/Silverlight/WPF/WP7/HTML5周学习导读(1月7日-1月14日)
查看>>
关于C#导出 文本文件
查看>>
使用native 查询时,对特殊字符的处理。
查看>>
maclean liu的oracle学习经历--长篇连载
查看>>
ECSHOP调用指定分类的文章列表
查看>>
分享:动态库的链接和链接选项-L,-rpath-link,-rpath
查看>>