添加删除记录及简单验证

最近做页面动态添加数据,根据网友发布的经验整合一下,可实现基本功能。晒一晒,分享一下。本人初学编程,代码难免冗余复杂,多多包含。

//js部分

<script language="javascript">

function checkAdd()
{
//验证不为空
if(document.formAdd.txtCountry.length!=null){
for(i= 0;i<document.formAdd.txtCountry.length;i++){
 if(document.formAdd.txtCountry[i].value==null||document.formAdd.txtCountry[i].value==""){
 alert("国家代码不能为空");
 document.formAdd.txtCountry[i].style.cssText += (';' + "background-color:blue;");
 document.formAdd.txtCountry[i].focus();
 return false;
  }
 }
}else{
 if(document.formAdd.txtCountry.value==null||document.formAdd.txtCountry.value==""){
 alert("国家代码不能为空");
 document.formAdd.txtCountry.style.cssText += (';' + "background-color:blue;");
 document.formAdd.txtCountry.focus();
 return false;
 }
}
if(document.formAdd.txtChinese.length!=null){
for(i= 0;i<document.formAdd.txtChinese.length;i++){
 if(document.formAdd.txtChinese[i].value==null||document.formAdd.txtChinese[i].value==""){
 alert("中文简称不能为空");
 document.formAdd.txtChinese[i].style.cssText += (';' + "background-color:blue;");
 document.formAdd.txtChinese[i].focus();
 return false;
  }
 }
}else{
 if(document.formAdd.txtChinese.value==null||document.formAdd.txtChinese.value==""){
 alert("中文简称不能为空");
 document.formAdd.txtChinese.style.cssText += (';' + "background-color:blue;");
 document.formAdd.txtChinese.focus();
 return false;
 }
}
return true;
}

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[i][theObj];
for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
    foundObj = findObj(theObj,theDoc.layers[i].document);
if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
return foundObj;
}

//添加一个参与人填写行
function AddSignRow(){
 //checkadd();

//读取最后一行的行号,存放在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 = "<input name='txtCountry' id='txtCountry' type='text' size='12' />";
//newNameTD.innerHTML = "<textarea name='txtKey'   id='txtKey'  style='width:180px;height:80px;'></textarea>";

//添加列:包含
var newEmailTD=newTR.insertCell(2);
//添加列内容
newEmailTD.innerHTML = "<input name='txtChinese'   id='txtChinese'  type='text' size='12'/>";

//添加列:不包含
var newTelTD=newTR.insertCell(3);
//添加列内容
newTelTD.innerHTML = "<input name='txtFull'   id='txtFull'  type='text' size='12'/>";

var newTelTD=newTR.insertCell(4);
//添加列内容
newTelTD.innerHTML = "<input name='txtNum'   id='txtNum'  type='text' size='12'/>";
//添加列:删除按钮
var newDeleteTD=newTR.insertCell(5);
//添加列内容
newDeleteTD.innerHTML = "<div align='center' style='width:40px'><a href='javascript:;' οnclick=/"DeleteSignRow('SignItem" + rowID + "')/">删除</a></div>";

//将行号推进下一行
txtTRLastIndex.value = (rowID + 1).toString() ;
}

//删除指定行
function DeleteSignRow(rowid){
var signFrame = findObj("SignFrame",document);
var signItem = findObj(rowid,document);
//alert(signItem);
//获取将要删除的行的Index
var rowIndex = signItem.rowIndex;

if(signFrame.rows.length==2){
 alert(" 已是最后一行,不能删除!");
 return false;
}

//删除指定Index的行
signFrame.deleteRow(rowIndex);

//重新排列序号,如果没有序号,这一步省略
for(i=rowIndex;i<signFrame.rows.length;i++){
   signFrame.rows[i].cells[0].innerHTML = i.toString();
}

}
//清空列表
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();
}
}

</script>

 

//html部分

 <form name="formAdd" method="post" action="DataService/InsertCountryCode.jsp" οnsubmit="return checkAdd();">
   <div class="pageTitle">
    <h1>
     数据维护
     <span class="point">&gt;&gt;</span>国家代码表 
     <span class="point">&gt;&gt;</span>添加规则
         
    </h1>
   </div>
   <div class="page_Main">
    <table>
    <tr>
    <td>
     <table width="100%" border="0" cellpadding="2" cellspacing="1" id="SignFrame">
                  <tr id="trHeader">
                <th width="5%">序号</th>
                <th width="15%">国家代码</th>
                <th width="15%">中文简称</th>
                <th width="25%">中文全称</th>
                <th width="25%">数字代码</th>                 
                 <th width="15%">操作</th>
               </tr>
       <tr>
       <td>1</td>
       <td><input name='txtCountry' id='txtCountry' type='text' size='12' /></td>
       <td><input name='txtChinese'   id='txtChinese'  type='text' size='12'/></td>
       <td><input name='txtFull'   id='txtFull'  type='text' size='12'/></td>
       <td><input name='txtNum'   id='txtNum'  type='text' size='12'/></td>
       
       </tr> 
      </table>
     </td>
    </tr>
    <tr>
     <td>
      <div>
             <input type="button" name="Submit" value="继续添加" οnclick="AddSignRow()" />
           <input type="button" name="Submit2" value="清空" οnclick="ClearAllSign()" />
           <input name='txtTRLastIndex' type='hidden' id='txtTRLastIndex' value="1" />
        </div>
     </td>
    </tr>
    
    <tr>
     <td align="right"><input type="submit" name="Submit" value="确定更改" />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     <input type="reset" name="Reset" value="重置" /></td>
    </tr>
   </table>   
   
   </div>
  </form>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值