求代码:
   需求: 用js验证文本框内所填入的字符串是否重复,入重复需在文本框后显示名字已存在。谢谢各位,帮帮忙!

解决方案 »

  1.   

    可是实现的。. 首先页面文本框, 
     
               <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">&nbsp;不能为空</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
        }}
    自己在理清思路,慢慢改..我现在做的项目就是用这种. 不过还有更简单的.就不说了..
      

  2.   

    没那,你的太复杂了,没有用的方法。我现在就是在serviceimpl判断一下,然后在页面得到action。
      

  3.   

    AJAX的无刷新机制使得在注册系统中对于注册名称的检测能即时显示。常见的用户注册是用户输入用户名,后台程序检测数据库中用户名是否重复而做出注册的成功与失败之提示(当用户注册重名时将返回重新注册),或者稍微人性化一点就是在用户名文本框后添加一个检测按钮,让用户检测后再做注册。以上操作,对于用户体验方面来说是比较“差劲”的,一个很好的用户体验就是:当用户输入完注册用户名后,Web系统应能即时检查并即时显示,并在检查和显示的同时不影响当前页面的操作。这也就是“异步获取数据”的要求,而这正是AJAX的强项比如如下的示例就能展现AJAX的该功能:http://www.cnbruce.com/test/ajax/t1.htm 当输入已经存在的用户名(如cnbruce、cnrose)时页面将显示重名不能注册(false),否则将显示可以注册(true),这为用户的注册提供了快速的参考,用户体验至上。那么下面就来说说是如何来实现这样的功能的。其实通过如上的t1.htm的源代码,各位就可以看到AJAX的精髓首先是定义XMLHttp对象
    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 
      

  4.   

    你说的这种就是Ajax来做,楼上给的可以仔细看看。可以上网搜一下ajax二级联动的小工程,自己看看就明白了
      

  5.   

    LZ看一下1L的代码应该可以实现的!