我的代码可以动态添加行 但是当删除当前行后 索引发生了变化
例如 当前行我的控件的ID是txtContent2 当删除后该行后 索引txtContent2被删除 txtContext3到了txtContent2的位置
所以就变成了 txtContent1 txtContent3...
而不是按顺序输出 txtContent1 txtContent2...
我在程序中重新让他们按顺序输出 但是不起作用 大家帮忙看一下。 <script language="javascript">// Example: obj = findObj("image1");
function findObj(theObj, theDoc)
{
var p, i, foundObj;
if(!theDoc) theDoc = document;
if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
{ theDoc = parent.frames[theObj.substring(p+1)].document; theObj = theObj.substring(0,p); } if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj]; for (i=0; !foundObj && i < theDoc.forms.length; i++) foundObj = theDoc.forms[theObj]; for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) foundObj = findObj(theObj,theDoc.layers.document); if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj); return foundObj;
}
//添加一个参与人写好行
function AddSignRow(){ //读取最后一行的行号,存放在txtTRLastIndex文本框中
var txtTRLastIndex = findObj("txtTRLastIndex",document);
var rowID = parseInt(txtTRLastIndex.value);
var signFrame = findObj("SignFrame",document);
//添加行
var newTR = signFrame.insertRow(signFrame.rows.length);
newTR.id = "SignItem" + rowID;
//添加列:序号
var newNameTD=newTR.insertCell(0);
//添加列内容
newNameTD.innerHTML = newTR.rowIndex.toString();
//添加列:工作内容及完成情况
var newNameTD=newTR.insertCell(1);
//添加列内容
newNameTD.innerHTML = "<textarea name='txtContent" + rowID + "' id='txtContent" + rowID + "' rows='4' cols='40' />";
//添加列:成果
var newEmailTD=newTR.insertCell(2);
//添加列内容
newEmailTD.innerHTML = "<textarea name='txtResult" + rowID + "' id='txtResult" + rowID + "' rows='4' cols='40' />";
//添加列:删除按钮
var newDeleteTD=newTR.insertCell(3);
//添加列内容
newDeleteTD.innerHTML = "<div align='center' style='width:40px'><a href='javascript:;' onclick=\"DeleteSignRow('SignItem" + rowID + "')\">删除</a></div>";
//将行号推进下一行
txtTRLastIndex.value = (rowID + 1).toString() ;
}
//删除指定行
function DeleteSignRow(rowid){
var signFrame = findObj("SignFrame",document);
var signItem = findObj(rowid,document);
//获取将要删除的行的Index
var rowIndex = signItem.rowIndex;
//删除指定Index的行
signFrame.deleteRow(rowIndex);
//重新排列序号,如果没有序号,这一步省略
for(i=rowIndex;i<signFrame.rows.length;i++){//将所有input的编号进行重新分配
//要将所有的列都写上
//var old_id=signFrame.rows[i].cells[1].childNodes[0].id;//name
//7为列名txtName的长度
//var new_id=old_id.substr(0,10) + i.toString();
//signFrame.rows[i].cells[1].childNodes[0].id=new_id;
//alert(signFrame.rows[i].cells[1].innerHTML);
//signFrame.rows[i].cells[1].childNodes[0].value=new_id;
//document.getElementById(new_id).value=new_id;
//================================================
signFrame.rows[i].cells[0].innerHTML = i.toString();
}
//重新绑定 但不起作用 为什么呢????
var signFrame = findObj("SignFrame",document);
var rowscount = signFrame.rows.length;
for(i=1;i <= rowscount; i++){
var new_id=old_id.substr(0,10) + i.toString();
signFrame.rows[i].cells[1].childNodes[0].id=new_id;
}
}
//清空列表
function ClearAllSign(){
if(confirm('确定要清空所有列表内容吗?')){
var signFrame = findObj("SignFrame",document);
var rowscount = signFrame.rows.length;
//连续循环删除行,从最后一行往前删除
for(i=rowscount - 1;i > 0; i--){
signFrame.deleteRow(i);
}
//重置最后行号为1
var txtTRLastIndex = findObj("txtTRLastIndex",document);
txtTRLastIndex.value = "1";
//预添加一行
AddSignRow();
}
}<div>
<table border="1" cellpadding="0" cellspacing="0" id="SignFrame">
<tr id="trHeader">
<td>序号</td>
<td>工作内容及完成情况</td>
<td>成果</td>
<td align="center"> </td>
</tr>
</table>
</div>
<div>
<input type="button" name="Submit" value="添加" onclick="AddSignRow()" />
<input type="button" name="Submit2" value="清空" onclick="ClearAllSign()" />
<input name='txtTRLastIndex' type='hidden' id='txtTRLastIndex' value="1" />
</div>
例如 当前行我的控件的ID是txtContent2 当删除后该行后 索引txtContent2被删除 txtContext3到了txtContent2的位置
所以就变成了 txtContent1 txtContent3...
而不是按顺序输出 txtContent1 txtContent2...
我在程序中重新让他们按顺序输出 但是不起作用 大家帮忙看一下。 <script language="javascript">// Example: obj = findObj("image1");
function findObj(theObj, theDoc)
{
var p, i, foundObj;
if(!theDoc) theDoc = document;
if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
{ theDoc = parent.frames[theObj.substring(p+1)].document; theObj = theObj.substring(0,p); } if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj]; for (i=0; !foundObj && i < theDoc.forms.length; i++) foundObj = theDoc.forms[theObj]; for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) foundObj = findObj(theObj,theDoc.layers.document); if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj); return foundObj;
}
//添加一个参与人写好行
function AddSignRow(){ //读取最后一行的行号,存放在txtTRLastIndex文本框中
var txtTRLastIndex = findObj("txtTRLastIndex",document);
var rowID = parseInt(txtTRLastIndex.value);
var signFrame = findObj("SignFrame",document);
//添加行
var newTR = signFrame.insertRow(signFrame.rows.length);
newTR.id = "SignItem" + rowID;
//添加列:序号
var newNameTD=newTR.insertCell(0);
//添加列内容
newNameTD.innerHTML = newTR.rowIndex.toString();
//添加列:工作内容及完成情况
var newNameTD=newTR.insertCell(1);
//添加列内容
newNameTD.innerHTML = "<textarea name='txtContent" + rowID + "' id='txtContent" + rowID + "' rows='4' cols='40' />";
//添加列:成果
var newEmailTD=newTR.insertCell(2);
//添加列内容
newEmailTD.innerHTML = "<textarea name='txtResult" + rowID + "' id='txtResult" + rowID + "' rows='4' cols='40' />";
//添加列:删除按钮
var newDeleteTD=newTR.insertCell(3);
//添加列内容
newDeleteTD.innerHTML = "<div align='center' style='width:40px'><a href='javascript:;' onclick=\"DeleteSignRow('SignItem" + rowID + "')\">删除</a></div>";
//将行号推进下一行
txtTRLastIndex.value = (rowID + 1).toString() ;
}
//删除指定行
function DeleteSignRow(rowid){
var signFrame = findObj("SignFrame",document);
var signItem = findObj(rowid,document);
//获取将要删除的行的Index
var rowIndex = signItem.rowIndex;
//删除指定Index的行
signFrame.deleteRow(rowIndex);
//重新排列序号,如果没有序号,这一步省略
for(i=rowIndex;i<signFrame.rows.length;i++){//将所有input的编号进行重新分配
//要将所有的列都写上
//var old_id=signFrame.rows[i].cells[1].childNodes[0].id;//name
//7为列名txtName的长度
//var new_id=old_id.substr(0,10) + i.toString();
//signFrame.rows[i].cells[1].childNodes[0].id=new_id;
//alert(signFrame.rows[i].cells[1].innerHTML);
//signFrame.rows[i].cells[1].childNodes[0].value=new_id;
//document.getElementById(new_id).value=new_id;
//================================================
signFrame.rows[i].cells[0].innerHTML = i.toString();
}
//重新绑定 但不起作用 为什么呢????
var signFrame = findObj("SignFrame",document);
var rowscount = signFrame.rows.length;
for(i=1;i <= rowscount; i++){
var new_id=old_id.substr(0,10) + i.toString();
signFrame.rows[i].cells[1].childNodes[0].id=new_id;
}
}
//清空列表
function ClearAllSign(){
if(confirm('确定要清空所有列表内容吗?')){
var signFrame = findObj("SignFrame",document);
var rowscount = signFrame.rows.length;
//连续循环删除行,从最后一行往前删除
for(i=rowscount - 1;i > 0; i--){
signFrame.deleteRow(i);
}
//重置最后行号为1
var txtTRLastIndex = findObj("txtTRLastIndex",document);
txtTRLastIndex.value = "1";
//预添加一行
AddSignRow();
}
}<div>
<table border="1" cellpadding="0" cellspacing="0" id="SignFrame">
<tr id="trHeader">
<td>序号</td>
<td>工作内容及完成情况</td>
<td>成果</td>
<td align="center"> </td>
</tr>
</table>
</div>
<div>
<input type="button" name="Submit" value="添加" onclick="AddSignRow()" />
<input type="button" name="Submit2" value="清空" onclick="ClearAllSign()" />
<input name='txtTRLastIndex' type='hidden' id='txtTRLastIndex' value="1" />
</div>
解决方案 »
- 日期格式的js问题
- 关于 confirm简单问题~~~
- 【一条语句,firefox可以正确显示,ie无反应,我疯啊!!!!】
- JS对iframe操作的问题(无法取到对象)
- $.post 第一个参数多出[] 不知道为啥
- [求助]关于登录窗口的问题??
- 在线Html编辑器中添加表格的功能是怎麽实现的?execCommand参数列表中没有关于table的阿?
- 这个网页元素checkbox我怎么访问?急!
- jqgrid中form表单属性设置以及如何提交表单后自动关闭form表单
- Js九九乘法表怎么反过来
- 求助!!如何开发可以自由拖动页面布局页面
- 求JS比较输入的字符串,相同就跳转到a.asp,不同就不跳转
//删除指定行
function DeleteSignRow(rowid){
var signFrame = findObj("SignFrame",document);
var signItem = findObj(rowid,document);
//获取将要删除的行的Index
var rowIndex = signItem.rowIndex;
//删除指定Index的行
signFrame.deleteRow(rowIndex);
//重新排列序号,如果没有序号,这一步省略
for(i=rowIndex;i<signFrame.rows.length;i++){//将所有input的编号进行重新分配
//要将所有的列都写上
//var old_id=signFrame.rows[i].cells[1].childNodes[0].id;//name
//7为列名txtName的长度
//var new_id=old_id.substr(0,10) + i.toString();
//signFrame.rows[i].cells[1].childNodes[0].id=new_id;
//alert(signFrame.rows[i].cells[1].innerHTML);
//signFrame.rows[i].cells[1].childNodes[0].value=new_id;
//document.getElementById(new_id).value=new_id;
//================================================
signFrame.rows[i].cells[0].innerHTML = i.toString();
}
------------------------------------------------------
这里是重新生成 但没起作用 我从后台取不到
var signFrame = findObj("SignFrame",document);
var rowscount = signFrame.rows.length;
for(i=1;i <= rowscount; i++){
//var old_id=signFrame.rows[i].cells[1].childNodes[0].id;
var new_id='txtContent' + i.toString();
signFrame.rows[i].cells[1].childNodes[0].id=new_id;
}
---------------------------------------------------------
}
不过这边的索引是不是该从0开始呢
signFrame.rows[i].cells[1].childNodes[0].id=new_id;
而不是txtContentID1 txtContentID3.