HTML的table结构如下:
复制代码 代码如下:
<table id="Dy_table" width="760" cellpadding="0" style="border-top: solid 1px #9cf"
class="tableStyle1" cellspacing="0">
<tr>
<th style="width: 40px">序号<input id="pageRows" name="pageRows" type="hidden" value="1" />
<input type="hidden" name="HF_tableRows" id="HF_tableRows" value="1" /></th>
<th style="width:120px">零件型号</th>
<th style="width:130px">零件名称</th>
<th style="width:45px">数量</th>
<th style="width:70px">无税价</th>
<th style="width:70px">含税价</th>
<th style="width:70px">税额</th>
<th style="width:70px">货款</th>
<th style="width:70px">整额</th>
<th style="width:60px">操作</th>
</tr>
<tr>
<td>1</td>
<td><input name='Dy_text_ljh' style='width: 110px' ondblclick='selectLj(this)' type='text'
readonly='true' /><input name='Dy_hd_cpdm' type='hidden' onpropertychange='textChange(this)'
title='产品代码' /><input name='Dy_hd_mxid' type='hidden' value='' title='该行的Id,用来修改和删除' /><input
name='Dy_hd_rowState' type='hidden' value='1' title='该行的状态' /><input name='Dy_hf_ljgg'
type='hidden' value='0' title='零件规格' /></td>
<td><input name='Dy_text_cpmc' style='width: 120px' readonly='true' type='text' /></td>
<td><input name='Dy_text_sl' value='1' onkeypress='onlyNumberIn1(this)' onkeyup='textChange(this)'
style='width: 35px' type='text' /></td>
<td><input name='Dy_text_wsj' style='width: 60px' type='text' readonly='true' /></td>
<td><input name='Dy_text_hsj' style='width: 60px' type='text' readonly='true' /></td>
<td><input name='Dy_text_se' style='width: 60px' type='text' readonly='true' /></td>
<td><input name='Dy_text_hk' style='width: 60px' type='text' readonly='true' /></td>
<td><input name='Dy_text_ze' style='width: 60px' type='text' readonly='true' /></td>
<td><input name='del' type='button' value='删 除' class='input-button' onclick='delnode1(this)' /></td>
</tr>
</table>
js代码如下:
复制代码 代码如下:
function addEvent (o,c,h){
if(o.attachEvent){
o.attachEvent('on'+c,h);
}else{
o.addEventListener(c,h,false);
}
return true;
}
var selectRow;//页面级js变量,用来存被选中的行,好在弹出窗口中对该行赋值
function addnode(){
var table=document.getElementById("Dy_table");
var tr=table.rows[1].cloneNode(true);
for(var i=1;i<tr.childNodes.length-1;i++){
for(var p=0;p<tr.childNodes[i].getElementsByTagName("input").length;p++){
if(tr.childNodes[i].getElementsByTagName("input")[p].name=="Dy_hd_rowState")//行状态特殊对待
tr.childNodes[i].getElementsByTagName("input")[p].value="1";
else
tr.childNodes[i].getElementsByTagName("input")[p].value="";
}
}
var rowCount = table.rows[0].cells[0].getElementsByTagName("input")[1].value;//用户可见的行数
tr.firstChild.innerHTML=parseInt(rowCount)+1;
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount)+1;//可见行数+1
table.rows[0].cells[0].getElementsByTagName("input")[0].value = table.rows.length;//总行数,包含隐藏的
var tbody=table.getElementsByTagName("tbody");
if(tbody!=null){
tbody[0].appendChild(tr);
}else
table.appendChild(tr);
}
//删除时的事件
function delnode(){
var table=document.getElementById("Dy_table");
var rowCount = table.rows[0].cells[0].getElementsByTagName("input")[1].value;//用户可见的行数
var row;//获取最后一个可见的row
for( var i=table.rows.length-1; i>=0 ;i--){
if(table.rows[i].style.display!="none")
{
row=table.rows[i];
break;
}
}
var rowId=row.cells[1].getElementsByTagName("input")[2].value;
if( rowCount > 1 ){
if(rowId=="")//新增的行未写入数据库时,直接删除
{
var tbody=table.getElementsByTagName("tbody");
if(tbody!=null){
tbody[0].removeChild(row);
}else
table.removeChild(row);
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount) - 1;
}
else//需要从数据库删除的,置上删除标记
{
row.style.display="none";
row.cells[1].getElementsByTagName("input")[3].value = "2";
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount)-1;
}
}else{
if(rowId == ""){//新增的行未写入数据库时,清空
row.cells[1].getElementsByTagName("input")[0].value="";
row.cells[1].getElementsByTagName("input")[1].value="";
row.cells[1].getElementsByTagName("input")[2].value="";
row.cells[1].getElementsByTagName("input")[3].value="";
row.cells[1].getElementsByTagName("input")[4].value="";
row.cells[2].getElementsByTagName("input")[0].value="";
row.cells[3].getElementsByTagName("input")[0].value="1";
row.cells[4].getElementsByTagName("input")[0].value="";
row.cells[5].getElementsByTagName("input")[0].value="";
row.cells[6].getElementsByTagName("input")[0].value="";
row.cells[7].getElementsByTagName("input")[0].value="";
row.cells[8].getElementsByTagName("input")[0].value="";
}else{//需要从数据库删除的,置上删除标记
row.style.display="none";
row.cells[1].getElementsByTagName("input")[3].value = "2";
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount) - 1;
addnode();
}
}
setClf();
}
//删除时的事件
function delnode1(o){
var tr=o.parentNode.parentNode;
var table=document.getElementById("Dy_table");
var rowCount = table.rows[0].cells[0].getElementsByTagName("input")[1].value;//用户可见的行数
var rowId=tr.cells[1].getElementsByTagName("input")[2].value;
if( rowCount > 1 ){
if(rowId=="")//新增的行未写入数据库时,直接删除
{
var tbody=table.getElementsByTagName("tbody");
if(tbody!=null){
tbody[0].removeChild(tr);
}else
table.removeChild(tr);
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount) - 1;
}
else
{
tr.style.display="none";
tr.cells[1].getElementsByTagName("input")[3].value = "2";
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount) - 1;
}
}else{
if(rowId==""){//新增的行未写入数据库时,直接清空
tr.cells[1].getElementsByTagName("input")[0].value="";
tr.cells[1].getElementsByTagName("input")[1].value="";
tr.cells[1].getElementsByTagName("input")[2].value="";
tr.cells[1].getElementsByTagName("input")[3].value="";
tr.cells[1].getElementsByTagName("input")[4].value="";
tr.cells[2].getElementsByTagName("input")[0].value="";
tr.cells[3].getElementsByTagName("input")[0].value="1";
tr.cells[4].getElementsByTagName("input")[0].value="";
tr.cells[5].getElementsByTagName("input")[0].value="";
tr.cells[6].getElementsByTagName("input")[0].value="";
tr.cells[7].getElementsByTagName("input")[0].value="";
tr.cells[8].getElementsByTagName("input")[0].value="";
}else{//需要从数据库删除的,置上删除标记
tr.style.display="none";
tr.cells[1].getElementsByTagName("input")[3].value = "2";
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount) - 1;
addnode();
}
}
//以下循环用于从中间删除时更新表格行号
for( var i= 1,p = 1; i < table.rows.length ;i++){
if(table.rows[i].style.display!="none")
{
table.rows[i].cells[0].innerHTML = p;
p++;
}
}
setClf();
}
//修改时发生的事件,改变行状态
function textChange(o){
var tr=o.parentElement.parentElement;
if(o.parentElement.parentElement.parentElement==null)return;//如果是新增加的行则返回
var rowState = tr.cells[1].getElementsByTagName("input")[3].value;
if( rowState == "1")
return;
else
tr.cells[1].getElementsByTagName("input")[3].value = "3";
setClf();
}
//提交前验证数据,保证没有重复的行
function checkSameData(){
var table=document.getElementById("Dy_table");
for( var i= 1; i < table.rows.length ;i++){
if(table.rows[i].style.display == "none"||table.rows[i].cells[1].getElementsByTagName("input")[1].value=="") continue;
for( var p= i + 1; p < table.rows.length ;p++){
if(table.rows[p].style.display == "none") continue;
if(table.rows[i].cells[1].getElementsByTagName("input")[1].value.replace(/\s+$/g,"") ==
table.rows[p].cells[1].getElementsByTagName("input")[1].value.replace(/\s+$/g,""))
{alert("零件部分存在重复的项,不能保存!");return false;}
}
}
return true;
}
var dialogWin;//零件窗口是否打开
//选零件
function selectLj(o){
if(dialogWin == null){
selectRow = o.parentNode.parentNode;//将行赋值给全局变量
var cpxh = selectRow.cells[1].getElementsByTagName("input")[0].value;
dialogWin = winOpen('selectLj.aspx?ljh='+cpxh);
// window.open("../jddgl/Select_lj.aspx?ljh=" + cpxh,window,
// "center:yes;dialogWidth:600px;dialogHeight:400px;help:no;status:no;");
}
}
function winOpen(url){
return window.open(url,'selectLj','resizable=1,status=0,menubar=0,scrollbars=1,height=400px,width=600px');
}
//计算table内费用
function setClf(){}
这算是对之前写的动态增加表格的改进,之前那个实在是学习了js没多久而作的失败作品。现在这个总算是可以兼容FF和IE了。在兼容的过程中,没少看标准dom规范,提高了不少知识,js操作dom翻看ms的DHTML手册的时候要注意它里面提到的方法和属性是不是标准的,最好用标准的。
此动态table只要HTML里定好了table就可以动态的增减,不用关心它有多少个td,注意在第二个td里面加上相关的input hidden。动态增删只是一个表面的功能,这个table和dataset一样具有一个行状态,用行状态可以在服务器端对数据进行更新、删除和新增。1新增,2删除,3修改。只是用弹出窗口在FF和IE7下效果不行,不知道用iframe效果怎么样。
IE下对于clone出来的tr不能通过cells来获取td的集合,FF下则是可以。由于tr内有input用了onpropertychange事件,在去掉新增的tr内input值的时候也会触发,所以在这个事件里用一个if排除了这种情况。浏览器的兼容还真是有些麻烦。FF下面还存在一个问题,从没有提交页面的情况下,FF重新载入页面的时候,服务器端控件的值会被保存下来,而IE下则是真的重新载入,页面上的任何值都不会保留。FF的这个保存服务器控件值的行为应该是它对asp.net支持存在问题,没有提交页面的情况下这是不应该发生的。
相关推荐:
SEO实际:如何在竞争激烈的市场中脱颖而出,dz论坛seo如何设置
为什么选择收录网站是企业在线营销的必备策略,全网营销与推广
SEO很多,如何在竞争激烈的市场中脱颖而出?,在SEO优化中
ChatGPTWindows版本下载:让AI助力您的工作和生活,ai yamama
seo软件工具,seo软件是什么 ,AI妆容
SEO优化基础:让你的网站脱颖而出的秘密武器,模仿猫ai
SEO拓客,让您的业务飞速增长!,密云自适应网站建设
SEO通过-如何通过SEO优化让你的业务在搜索引擎中脱颖而出,保定网站建设推广专家
SEO努力:提升网站排名的关键策略,网站建设微企动力
SEO利用:让你的网站快速登顶,获取更多流量与客户!,延边小程序推广平台网站
AI助手Stut:智能时代的创新引擎,开启未来工作新篇章,ai对ai的格斗游戏
SEO在线服务-让您的网站快速跃升至搜索引擎前列,江西小红书营销推广案例
SEO化学物质:如何在化工行业提升搜索引擎排名和品牌曝光,rack ai
AI写作技巧,让创作事半功倍!
SEO怎么排名?这5大技巧,轻松提升网站排名,人人都可ai
ChatGPT维护页面-背后的技术与用户体验,ai领域ppt
OpenAIGPTChatSoraOpenAIChatGPT服务在中断数小时后已恢复,ai视图线稿
ChatGDP人工智能:未来科技赋能企业与个人的智能变革,如何用AI绘制人体
OpenPose大白话解释:让你轻松理解姿态识别的神奇技术,ai天天晴
为什么seo推广那么多,seo推广难吗 ,东莞ai听译平台
ChatGPT:智能对话开创新时代,ai做渐变直线
ChatGPT支持多种语言输入输出,让全球资讯触手可及,联想拯救者的ai写作
ChatGPT破解:人工智能的无限潜力与破解秘笈,意识变ai
SEO与SEM:谁才是提升网站流量的王者?,Ai中字体如何变形立体
SEO手段:提升网站流量的制胜法宝,idc网站怎么推广
OpenAI银联支付-未来支付新体验,安全便捷全新升级,医学加ai
seo软文有什么作用,seo文案是什么 ,斐乐Ai测评问题
在线AI文章生成:内容创作新革命
ChatGPT页面怎么拖不动?解决问题的终极指南,日韩AI换脸在线观看
怎么查一篇文章是不是AI写的?你需要这几个关键方法!
OpenAI智能诊断医疗-开启精准医疗新纪元,跟侯维静学ai
亚马逊产品seo什么意思,亚马逊平台产品专业术语 ,小新同学ai
什么是秀米?全方位了解这款微信公众号排版神器,宁波全网营销型网站建设
Bing搜索不能预览了?搜索引擎的新变革与挑战,ai制作一张窗花
SEO找词:如何精准找到高效关键词,提升排名和流量,河源网站优化平台
SEO前的准备工作:如何让网站为搜索引擎优化做好充分准备,SEO_网站排名优化_网络推广
ChatGPT403:引领人工智能新时代,颠覆你的工作与生活方式,ai nak
用AI创作的文章算原创吗?深度背后的逻辑与意义
AI撰写率:让创作变得更高效,助力内容产业腾飞,人力ai
seo营销到底是什么,seo算什么营销 ,ai里面新建
AI文章概括缩写:让内容高效获取的智能工具,ai 处理文件
ChatGPT4网页空白:重新定义智能交互的未来,怎么登ai
目前国内最好的AI人工智能软件:未来新篇章
SEO搜外:全球搜索引擎优化的秘密武器,如何选择网站建设类型呢
seo要会些什么,seo需要学些什么内容 ,轻盈ai
目前最火的AI软件有哪些?深度解析必备工具
SEO有意:如何通过优化策略提升网站排名与流量,天津政府智慧网站建设
ChatGPT40不收费版本:科技革新,智能助手助力生活与工作,ai医疗武器有哪些
互联网资源的无限潜力:如何利用数字世界为个人和企业创造价值,信阳网站建设正规公司
怎么分辨文章是不是AI写的?五大技巧揭开真相