我的意思是能不能把function ageTest()这个函数去掉,把这个函数下面原本的"年龄"判断条件合并到一个函数function ck() 里面.

解决方案 »

  1.   

        <script type="text/javascript" language="javascript"> 
            //增加一个TextBox
            function Add()
            {
                var table=document.getElementById("table");
                var num=document.getElementsByName("年龄").length;
                if(num>=2)
                {
                    var remove=document.getElementById("btnRemove");
                    remove.style.visibility="visible";
                }
                var tr=table.insertRow(table.rows.length-2);
                var td1=tr.insertCell();
                var td2=tr.insertCell();
                var td3=tr.insertCell();
                var td4=tr.insertCell();
                //var IDVal="性别"+(num+1);
                //alert(IDVal);
                td1.innerText="姓名"+(num+1);
                td2.innerHTML="<input type='Text' name='姓名' id='姓名"+(num+1)+"' size='18'>";
                td3.innerText="*性别"+(num+1);
                td4.innerHTML="<input type='Text' name='年龄' id='年龄"+(num+1)+"' size='32'>";
                //alert(td2.innerHTML);
            }
            
            //移去最后一个TextBox
            function Remove()
            {
                var table=document.getElementById("table");
                var num=document.getElementsByName("年龄").length;
                if(num<=3)
                {
                    var remove=document.getElementById("btnRemove");
                    remove.style.visibility="hidden";
                }
                table.deleteRow(table.rows.length-3);
            }
    function ck() 

        
         var msg="信息确认: \n \n"; 
         var itemname=document.getElementById("itemname").value; 
            if (itemname=="")
                {
                      alert("学校不能为空!");
                      document.getElementById("itemname").value=""; 
                    document.getElementById("itemname").focus(); 
                      return false;
                 }
            else 
            { 
                msg+="学校 : "+itemname+"\n"; 
                //获取所有input文本框
                var inputList = document.getElementsByTagName("INPUT");            for (var i = 0; i < inputList.length; i++) {
                    if (inputList[i].type == "text" || inputList[i].id != "itemname") {
                        if (inputList[i].name == "姓名") {
                            msg += "姓名 : " + inputList[i].value; 
                        }                    if (inputList[i].name == "年龄") {
                            msg += "年龄 : " + inputList[i].value + "\n"; 
                        }
                    }
                }
                    
                if(ageTest())
                 {
                    if(confirm(msg))
    {
                        return true;
                    }else
    {
                     return false; 
                    }              }            }
        
            return true;
    }function ageTest()
    {
        var ageList=document.getElementsByName("年龄");
        
        for(i=0;i<ageList.length;i++)
        {
            if(ageList[i].value.length >2 || isNaN(ageList[i].value) || ageList[i].value=="")
            {
                alert("年龄"+(i+1)+"不可为空,且为2位数字");
                ageList[i].focus();
                return false;
            }
         }     return true;
       //  MyForm.submit(); 
    }    </script>
      

  2.   

    function ageTest()
    {
        var ageList=document.getElementsByName("年龄");
        
        for(i=0;i<ageList.length;i++)
        {
            if(ageList[i].value.length >2 || isNaN(ageList[i].value) || ageList[i].value=="")
            {
                alert("年龄"+(i+1)+"不可为空,且为2位数字");
                ageList[i].focus();
                return false;
            }
         }     return true;//这里都return了,下面代码有什么用
         MyForm.submit(); 
    }
      

  3.   

    function ck(objForm) {     
         if (document.getElementById("itemname").value==""){
            alert("学校不能为空!");
            document.getElementById("itemname").focus(); 
            return 
         }
         
        var ageList=document.getElementsByName("年龄");
        for(i=0;i<ageList.length;i++){
            if(ageList[i].value.length >2 || isNaN(ageList[i].value) || ageList[i].value==""){
                alert("年龄"+(i+1)+"不可为空,且为2位数字");
                ageList[i].focus();
                return 
            }
         }
         
     var msg="信息确认: \n \n"; 
         msg+="学校 : "+itemname+"\n"; 
         var inputList = document.getElementsByTagName("INPUT");//获取所有input文本框
         for (var i = 0; i < inputList.length; i++) {
             if (inputList[i].type == "text" || inputList[i].id != "itemname") {
                 if (inputList[i].name == "姓名") {
                     msg += "姓名 : " + inputList[i].value; 
                 }
                 if (inputList[i].name == "年龄") {
                     msg += "年龄 : " + inputList[i].value + "\n"; 
                 }
             }
          }
          alert(msg)
          objForm .action="a.asp"
          objForm .submit()
    }</script> 
    <form  method="post" name="RegForm" onsubmit="ck(this)">
      

  4.   

    你把后面的MyForm.submit(); 放到
    if(confirm(msg)){
                        MyForm.submit(); (return true;)
                    }else{
                     return false; 
                    }
    换下就行了~