jsp中jquery用法一步刷新 验证用户名是否存在
<script type="text/javascript"> /* $(document).ready(function(){ var id="ha"; var s="<td>用户名:<select name='"+id+"'>"+"<option>sdfsdf</option></select>" $("#f").html(s); }) */ function func(){ document.form1.action="index.jsp?opt=0"; document.form1.submit(); } function funcs(){ if(!document.getElementById("name").value) { document.getElementById("name").value='请输入用户名';document.getElementById("name").style.color='#999'; } else{ var name=document.getElementById("name").value; $.ajax({ type: "POST", url:"my.jsp", data:"name="+name, success:function(name){ document.getElementById("tip").innerHTML=name; } }); } } function focus(){ alert("wocaonima"); // if(document.getElementById("user.name").value=='input') document.getElementById("user.name").value=""; if(document.getElementById("name").value=='input') { document.getElementById("name").value=""; alert("haobuhao1"); this.style.color='#000'; } } </script> <!-- onfocus="if(value=='请输入用户名') {value='';this.style.color='#000';}" onblur="if(!value) {value='请输入用户名';this.style.color='#999';}" --> <% if(session.getAttribute("username")==null) { %> <body> <center> <form method="post" name="form1"> <table> <tr><div id="f"><td>用户名:<input type="text" id="name" name="name" onfocus="if(value=='请输入用户名') {value='';this.style.color='#000';}" onblur="funcs()"></td></div><td><label id="tip"></label></tr> <tr><td>密码:<input type="password" name="password"></td></tr> <tr><td> <input type="button" value="提交" onclick="func()" align="middle"> </td></tr> </table></form> </center> </body> <%} else{ %> <script type="text/javascript"> window.location.href="index.jsp"; </script> <% } %>
my.jsp为ajax的处理页面,代码如下
<%@page import="java.io.PrintWriter"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="com.gree.ConnDB" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <% String name= request.getParameter("name"); String data=""; int result=0; // response.setContentType("text/html"); PrintWriter outs=response.getWriter(); if(name==null) { name=""; System.out.print("haha"); } ConnDB db=new ConnDB(); result=db.test(name); if(result==0) data="<p style='color:red;'> 该用户名不存在!</p>"; else data="<p style='color:blue;'> 用户名输入正确</p>"; outs.write(data); %>
此处PrintWriter类的write方法可以将处理后的数据返还给login.jsp