求代码:
需求: 用js验证文本框内所填入的字符串是否重复,入重复需在文本框后显示名字已存在。谢谢各位,帮帮忙!
需求: 用js验证文本框内所填入的字符串是否重复,入重复需在文本框后显示名字已存在。谢谢各位,帮帮忙!
解决方案 »
- 请问JSTL如何获取java类中的静态变量
- 朋友们帮帮忙看看如何在本页打开这个页面呢?
- 【求助】js数组对象去除重复(存的是JSON对象的记录)
- JUnit比较两个null为什么不通过
- resin或者tomcat下的pdf文件中文名的都访问不了,是什么原因呢?
- 弱弱的问:我想测试一下我的网站会不会被sql注入?
- spring 的事务不回滚
- 弱弱的问:关于一个falsh的问题?
- 救命:帮我把String型xml ,用xml解析出来,不要分割字符串的?????
- 如果一个string型的变量为"aaa,bbb,ccc,ddd,eee,fff"怎么样把数据放入数组
- 在浏览器中查看jsp文件代码
- 网页中的falsh怎么总是下不全
<tr>
<td align="center" bgcolor="#F6F6F6">新闻标题:</td>
<td height="25" colspan="2" align="left" bgcolor="#F6F6F6" style="width: 367px">
<input type="text" name="title" id="title" onblur="startRequestUsingPOST();" type="text"/>
<label id="message" style="color: red">该标题已存在</label>
<label id="lbltitle" style="color: red">标题不能为空</label> </td>
<td bgcolor="#F6F6F6"> 不能为空</td>
</tr>然后JS..
function startRequestUsingPOST() {
if(checktitle()==false)
{
return;
}
var title = document.getElementById("title").value;
createXMLHttpRequest();
xmlHttp.open("POST","CheckNtype",true);
xmlHttp.onreadystatechange = processResponse;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send("title=" + title);
}
function processResponse() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
var result = xmlHttp.responseText;
if(result==1){
document.getElementById("message").style.display="inline";
document.getElementById("btnadd").disabled="disabled";
}else
{
document.getElementById("message").style.display="none";
document.getElementById("btnadd").disabled="";
}
}
}
}
function checktitle()
{
var title=document.getElementById("title").value;
if(title=="")
{
document.getElementById("lbltitle").style.display="inline";
return false;
}else
{
document.getElementById("lbltitle").style.display="none";
return true;
}
}然后WEB.XML加上一段配置. <servlet>
<servlet-name>CheckNtype</servlet-name>
<servlet-class>com.zshome_admin.struts.action.news.CheckNtype</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckNtype</servlet-name>
<url-pattern>/CheckNtype</url-pattern>
</servlet-mapping>package com.zshome_admin.struts.action.news;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.zshome_admin.hbm.News;
import com.zshome_admin.hbm.NewsDAO;
import com.zshome_admin.hbm.NewsTypeDAO;
import com.zshome_admin.hbm.PersonenlistDAO;public class CheckNtype extends HttpServlet { /**
* Constructor of the object.
*/
public CheckNtype() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
response.setCharacterEncoding("utf-8");
String uname = request.getParameter("uname");
String title=request.getParameter("title");
String position=request.getParameter("position");
if(uname!=null){
NewsTypeDAO dao=new NewsTypeDAO();
boolean flg=dao.findSelByTitle(uname);
if(flg)
{
out.println(1);
}else
out.println(0);
}else if(title!=null)
{
NewsDAO ndao=new NewsDAO();
boolean flg=ndao.IsExceTitle(title);
if(flg)
{
out.print(1);
}else
out.print(0);
}else if(position!=null)
{
PersonenlistDAO perdao=new PersonenlistDAO();
boolean flg=perdao.IsExcePosition(position);
if(flg)
{
out.print(1);
}else
out.print(0);
}
out.flush();
out.close();
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}}
自己在理清思路,慢慢改..我现在做的项目就是用这种. 不过还有更简单的.就不说了..
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
关于这部分内容的说明请看:
http://www.cnbruce.com/blog/showlog.asp?cat_id=34&log_id=987 接着是自定义函数
function callServer() {
var u_name = document.getElementById("u_name").value;
if ((u_name == null) || (u_name == "")) return;
var url = "cu.asp?name=" + escape(u_name);
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = updatePage;
xmlHttp.send(null);
} 该函数的主要功能就是异步获得cu.asp的内容,在此前将先提取当前页表单元素“u_name”即用户名文本框zhogn 的值,通过cu.asp其后的参数及赋值而得到了不同的结果(true or false)。那么这里要说的即是cu.asp,他的主要功能就是接受URL参数name的值做内容显示,该内容最终被t1.htm异步获取。<!--cu.asp的源码示例--><!--#include file="conn.asp"-->
<%
name=request.querystring("name")
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from u_ser where u_name='"&name&"'"
rs.Open sql,conn,1,1
if rs.eof and rs.bof then
response.write("true")
else
response.write("false")
end if
rs.close
set rs=nothing
call CloseDatabase
%>
如何将异步获取的信息显示在当前页呢
function updatePage() {
if (xmlHttp.readyState < 4) {
test1.innerHTML="loading...";
}
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
test1.innerHTML=response;
}
} 其中xmlHttp.readyState中的readyState表示服务器在处理请求时的进展状况,其值分别有0-4,各有其说明情况,具体请参看:http://www.cnbruce.com/blog/showlog.asp?cat_id=34&log_id=718 使用DHTML中的innerHTML可显示信息在定义的 <span id="test1">是否能注册</span> 上。其余表单页面就不详叙了打包文件下载(右键选择另存为,下载后修改文件后缀名为rar,解压打开):http://www.cnbruce.com/test/ajax/ajax.htm