提交表单方案总结

1、方式一:表单提交:在此方式中需要置顶表单的action属性,将提交按钮的type属性改为submit。如下:
     < form name="userForm" target="_self" id="userForm" action=“#” method=“post”>
       <!-- 用户信息开始 -->
           <input name="userName" type="text" class="text1" id="userName"
                                size="20" maxlength="20">
           <input name="password" type="password" class="text1"
                                    id="password" size="20" maxlength="20">
         <!-- 用户信息结束 -->  

          <div align="center">
                    <!-- 提交按钮 -->
              <input name="btnAdd" class="button1" type="submit" id="btnAdd"
                        value="添加" onClick="addUser()">
          </div>

    </form>


2、方式二:使用JS提交表单,通过按钮触发JS事件,获取表单ID进行提交。如下:
    1)表单信息如下:
     < form name="userForm" target="_self" id="userForm">
                    <!-- 用户信息开始 -->
           <input name="userName" type="text" class="text1" id="userName"
                                size="20" maxlength="20">
           <input name="password" type="password" class="text1"
                                    id="password" size="20" maxlength="20">
          <!-- 用户信息结束 -->

          <div align="center">
                    <!-- 提交按钮 -->
              <input name="btnAdd" class="button1" type="button" id="btnAdd"
                        value="添加" onClick="addUser()">
          </div>

    </form>


    2)JS提交代码如下:
     function  addUser() {
          with  (document.getElementById("userForm")){
            action="user_add.jsp";
            method="post";
            method="post";
        }

    } 


3、 方式三:利用ajax进行提交,(当文本框失去焦点时触发提交事件)如下
    1)HTML代码:          < input name="userId" type="text" class="text1" id="userId" size="10" maxlength="10" onkeypress="userIdOnkeypress()" value="<%= userId %>onblur="validate(this)"><span id="spanUserId"></span>

    2)JS验证代码
      var  xmlHttp;
    function createXMLHttpRequest(){
        //表示当前浏览器不是ie,如ns,firefox
        if(window.XMLHttpRequest){
            xmlHttp=new XMLHttpRequest();
        }else if (window.ActiveXObject){
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    function validate(field){
        //alert(field.value);
        if (trim(field.value).length != 0 ){
            //创建AJAX核心对象XMLHttpRequest
            createXMLHttpRequest();
            
            var url="user_validate.jsp?userId="+trim(field.value)+"&time=" + new Date().getTime();
            //设置请求方式为Get,设置请求的URL,设置为异步提交
            xmlHttp.open("GET", url, true);
            
            //将方法地址复制给onreadystatechange属性
            xmlHttp.onreadystatechange=callback;
            //将请求信息发送到Ajax引擎
            xmlHttp.send(null);
        }
        else{
            document.getElementById("spanUserId").innerHTML="";
        }
    }
    function callback(){
        //Ajax引擎状态为成功
        if (xmlHttp.readyState==4){
            //HTTP协议状态为成功
            if (xmlHttp.status==200){
                if (trim(xmlHttp.responseText) != ""){
                    document.getElementById("spanUserId").innerHTML="<font color='red'>"+xmlHttp.responseText+"</font>";
                }else{
                    document.getElementById("spanUserId").innerHTML="";
                }
            }else{
                alert("请求失败,错误码="+xmlHttp.status)
            }
        }
    }
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值