表单提交

1.当输入用户名和密码为空的时候,需要判断。这时候就用到了校验用户名和密码,这个需要在jsp的前端页面写;


用submit提交:
在jsp的前端页面的头部插入一个js方法:
 function checkUser(){
   var result = $("#userid").val();
   var password =$("#userpassid").val();
   if(result == ""  ){
     alert("用户名不能为空");
     return false;
   }
   if(password == ""  ){
    alert("密码不能为空");
     return false;
   }else{
   return true;
   }
}

在form表单里写成这样:

<form id="formid"  name= "myForm" method = 'post'  action = 'add.html' onsubmit = "return checkUser();" >
            <table width="100%" border="0">
              <tr>
                <td  align="right">用户名</td>
                <td><input type="text" value="" class="text2" name = "username" id = "userid"/></td>
              </tr>
              <tr>
                <td width="60" height="40" align="right">密&nbsp;&nbsp;码&nbsp;</td>
                <td><input type="password" value="" class="text2" name = "userpass" id = "userpassid"/></td>
              </tr>
              <tr>
                <td><div class="c4">
                    <input type="submit" value="" class="btn2"  />

      </tr>

</form>

2用ajax传递参数(就不用form标签啦)

function ajaxSubmit (id){
	var bol=window.confirm("你确定要保存信息吗?");
	if(bol){
		 var result = $("#userid").val();
           var password =$("#userpassid").val(); $.ajax({ async : false, //请勿改成异步,下面有些程序依赖此请数据 type : "POST", data : {result :result,password :password},
              url : "add.html",
              dataType : 'json',
              success : function(json) { if (json == "0") { alert("保存失败!"); } else { alert("保存成功!"); window.location.reload(); } } }); } }

<button type="button" onclick="ajaxSubmit();">保存</button>

  

原文地址:https://www.cnblogs.com/jin-000/p/6373005.html