JS的form表单提交


<!DOCTYPE html>

<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="formjs.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .block{
            width: 420px;
            margin: 0 auto;
            padding-left: 40px;
            border: 1px dashed silver;
        }
        li{
            position: relative;
            list-style: none;
            line-height: 45px;
        }
        li label{
            display: inline-block;
            width: 90px;
            text-align: right;
            color: #3091ff;
            font-weight: bold;
        }
        li input[type="text"]{
            width: 200px;
            height: 30px;
            outline: none;
            border-radius: 5px;
            border: 1px solid silver;
            font-size: 18px;
        }
        li input[type="submit"]{
            width: 60px;
            margin-left: 110px;
            height: 25px;


        }
        li input[type="reset"]{
            width: 60px;
            height: 25px;
        }
        .error{
            position: absolute;
            display: none;
            white-space: nowrap;
            font-size: small;
            color: red;
        }
    </style>
</head>
<body>
<div class="block">
    <!--action="" 表单提交的路径-->
    <form action="" onsubmit="return subtxt()">
        <ul>
            <li>
                <label for="nickname">昵称:</label>
                <input type="text" class="content" name="nickname"/>
                <span class="error">*昵称不能为空!</span>
            </li>
            <li>
                <label for="name">姓名:</label>
                <input type="text" class="content" name="name"/>
                <span class="error">*姓名不能为空!</span>
            </li>
            <li>
                <label for="idcard">证件号:</label>
                <input type="text" class="content" name="idcard"/>
                <span class="error">*证件号不能为空!</span>
            </li>
            <li>
                <label for="age">年龄:</label>
                <input type="text" class="content" name="age"/>
                <span class="error">*年龄不能为空!</span>
            </li>
            <li>
                <label for="sex">性别:</label>
                <input type="radio" checked name="sex"/>男<input type="radio" name="sex"/>女
            </li>
            <li>
                <label for="pwd">密码:</label>
                <input type="text" class="content" name="pwd"/>
                <span class="error">*密码不能为空!</span>
            </li>
            <li>
                <label for="pwdnew">确认密码:</label>
                <input type="text" class="content" name="pwdnew"/>
                <span class="error">*密码不能为空!</span>
            </li>
            <li>
                <label for="address">地址:</label>
                <input type="text" class="content" name="address"/>
                <span class="error">*地址不能为空!</span>
            </li>
            <li>
                <input type="submit" value="注册" class="subinfo"/>
                <input type="reset" value="重置"/>
            </li>
        </ul>
    </form>
</div>
</body>

</html>






var arr=[];
window.onload=function (){
   //获取文本元素
    var context=document.getElementsByClassName("content");
    for(var i=0;i<context.length;i++){
        //当前元素失去焦点
         context[i].onblur=function (){
            /*this.name  为当前的元素名称*/
             if(this.name=="nickname")
             {
                //如果文本值为空  则显示提示信息
                 if(this.value=="")
                 {
                     document.getElementsByClassName("error")[0].style.display="inline-block";
                     this.style.borderColor="red";
                     arr[0]=false;
                 }
                 else{
                     arr[0]=true;
                 }
             }
             else if(this.name=="name")
             {
                 if(this.value=="")
                 {
                     document.getElementsByClassName("error")[1].style.display="inline-block";
                     this.style.borderColor="red";
                     arr[1]=false;
                 }
                 else
                 {
                     arr[1]=true;
                 }
             }
             else if(this.name=="idcard")
             {
                 if(this.value==""){
                     document.getElementsByClassName("error")[2].style.display="inline-block";
                     document.getElementsByClassName("error")[2].innerHTML="*证件号不能为空!"
                     this.style.borderColor="red";
                     arr[2]=false;
                 }
                 else
                 {
                     if(this.value.length!=18)
                     {
                         document.getElementsByClassName("error")[2].style.display="inline-block";
                         document.getElementsByClassName("error")[2].innerHTML="*证件号长度为18位!";
                         this.style.borderColor="red";
                         arr[2]=false;
                     }
                     else{
                         //取最后一位
                         var txt=this.value.substr(17,1);
                         if(isNaN(txt))
                         {
                             if(txt.toLowerCase()!="x")
                             {
                                 document.getElementsByClassName("error")[2].style.display="inline-block";
                                 document.getElementsByClassName("error")[2].innerHTML="*证件号格式不正确!";
                                 this.style.borderColor="red";
                                 arr[2]=false;
                             }
                             else
                             {
                                 arr[2]=true;
                             }
                         }
                         else{
                             arr[2]=true;
                         }
                     }
                 }
             }
             else if(this.name=="age")
             {
                 if(this.value=="")
                 {
                     document.getElementsByClassName("error")[3].style.display="inline-block";
                     document.getElementsByClassName("error")[3].innerHTML="*年龄不能为空!";
                     this.style.borderColor="red";
                     arr[3]=false;
                 }
                 else
                 {
                      if(isNaN(this.value))
                      {
                          document.getElementsByClassName("error")[3].style.display="inline-block";
                          document.getElementsByClassName("error")[3].innerHTML="*年龄格式不正确!";
                          this.style.borderColor="red";
                          arr[3]=false;
                      }
                      else
                      {
                          if(this.value<0||this.value>100)
                          {
                              document.getElementsByClassName("error")[3].style.display="inline-block";
                              document.getElementsByClassName("error")[3].innerHTML="*年龄不符合要求!";
                              this.style.borderColor="red";
                              arr[3]=false;
                          }
                          else
                          {
                              arr[3]=true;
                          }
                      }
                 }
             }
             else if(this.name=="pwd")
             {
                  if(this.value=="")
                  {
                      document.getElementsByClassName("error")[4].style.display="inline-block";
                      document.getElementsByClassName("error")[4].innerHTML="*密码不能为空!";
                      this.style.borderColor="red";
                      arr[4]=false;
                  }
                  else
                  {
                      if(this.value.length<5||this.length>10)
                      {
                          document.getElementsByClassName("error")[4].style.display="inline-block";
                          document.getElementsByClassName("error")[4].innerHTML="*密码长度为5-10位!";
                          this.style.borderColor="red";
                          arr[4]=false;
                      }
                      else
                      {
                        //判断密码的要求格式
                          arr[4]=true;
                      }
                  }
             }
             else if(this.name=="pwdnew")
             {
                 if(this.value=="")
                 {
                     document.getElementsByClassName("error")[5].style.display="inline-block";
                     document.getElementsByClassName("error")[5].innerHTML="*密码不能为空!";
                     this.style.borderColor="red";
                     arr[5]=false;
                 }
                 else
                 {
                     if(this.value.length<5||this.length>10)
                     {
                         document.getElementsByClassName("error")[5].style.display="inline-block";
                         document.getElementsByClassName("error")[5].innerHTML="*密码长度为5-10位!";
                         this.style.borderColor="red";
                         arr[5]=false;
                     }
                     else
                     {
                         //判断两次密码是否相等
                         var pwd=document.getElementsByClassName("content")[4].value;
                         if(this.value!=pwd)
                         {
                             document.getElementsByClassName("error")[5].style.display="inline-block";
                             document.getElementsByClassName("error")[5].innerHTML="*两次密码不一致!";
                             this.style.borderColor="red";
                             arr[5]=false;
                         }
                         else
                         {
                             arr[5]=true;
                         }
                     }
                 }
             }
             else{
                 if(this.value=="")
                 {
                     document.getElementsByClassName("error")[6].style.display="inline-block";
                     document.getElementsByClassName("error")[6].innerHTML="*地址不能为空!";
                     this.style.borderColor="red";
                     arr[6]=false;
                 }
                 else
                 {
                     arr[6]=true;
                 }
             }
         }
        //当前元素获得焦点  显示相反的造型
        context[i].onfocus=function (){
            if(this.name=="nickname"){
                document.getElementsByClassName("error")[0].style.display="none";
            }
            else if(this.name=="name")
            {
                document.getElementsByClassName("error")[1].style.display="none";
            }
            else if(this.name=="idcard")
            {
                document.getElementsByClassName("error")[2].style.display="none";
            }
            else if(this.name=="age")
            {
                document.getElementsByClassName("error")[3].style.display="none";
            }
            else if(this.name=="pwd")
            {
                document.getElementsByClassName("error")[4].style.display="none";
            }
            else if(this.name=="pwdnew")
            {
                document.getElementsByClassName("error")[5].style.display="none";
            }
            else{
                document.getElementsByClassName("error")[6].style.display="none";
            }
            this.style.borderColor="silver";
        }
    }
}
function subtxt(){
    //检查数组里面的值
   if(arr.length!=0&&arr.length==7)
   {
       for(var i=0;i<arr.length;i++)
       {
           if(arr[i]==false)
           {
               return false;
           }
       }
       return true;
   }
    else{
       return false;
   }
}

在使用JavaScript提交表单时,我们需要使用form的submit()方法来触发表单提交事件。传递参数的方法有多种。 1. GET方式传递参数:在form标签的action属性中设置要提交到的URL,并将参数直接附加在URL后面。例如: ```html <form id="myForm" action="http://example.com/myPage?param1=value1&param2=value2" method="GET"> <!-- 表单内容 --> </form> ``` 在提交表单时,浏览器会将表单的数据与URL中的参数一起发送给服务器。 2. 使用JavaScript动态构建URL参数:可以使用JavaScript来动态构建URL参数,并通过设置form的action属性来传递参数。例如: ```html <form id="myForm" action="http://example.com/myPage" method="GET"> <!-- 表单内容 --> </form> <script> var form = document.getElementById("myForm"); form.action = "http://example.com/myPage?param1=" + encodeURIComponent(value1) + "&param2=" + encodeURIComponent(value2); form.submit(); </script> ``` 在JavaScript中,我们可以使用encodeURIComponent()函数将参数值进行编码,以确保在URL中正确传递参数。 3. 使用POST方式传递参数:如果希望将参数作为请求的体部分发送,可以将表单的method属性设置为POST,并使用JavaScript来动态构建表单数据。例如: ```html <form id="myForm" action="http://example.com/myPage" method="POST"> <!-- 表单内容 --> </form> <script> var form = document.getElementById("myForm"); var formData = new FormData(form); formData.append("param1", value1); formData.append("param2", value2); var xhr = new XMLHttpRequest(); xhr.open("POST", form.action); xhr.send(formData); </script> ``` 在这种方法中,我们使用了FormData对象来存储表单数据,并使用XMLHttpRequest发送POST请求。通过append()方法,我们可以将额外的参数添加到formData对象中。 需要注意的是,使用这种方式传递的参数不会出现在URL中,而是作为请求的主体部分发送给服务器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值