zoukankan      html  css  js  c++  java
  • 使用ajax验证信息

    使用ajax验证信息

    1. controller

      @RequestMapping("/check")
      public String check(String username, String password) {
          String msg = "";
          if (username != null) {
              if ("admin".equals(username)) {
                  msg = "ok";
              } else {
                  msg = "用户名错误";
              }
          }
          if (password != null) {
              if ("123456".equals(password)) {
                  msg = "ok";
              } else {
                  msg = "密码错误";
              }
          }
          return msg;
      }
      
    2. username,password输入框和提示

      <p>
          用户名:<input type="text" id="username" onblur="checkid()">
          <span id="userInfo"></span>
      </p>
      <p>
          密码:<input type="password" id="password" onblur="checkpwd()">
          <span id="pwdInfo"></span>
      </p>
      
    3. checkid和checkpwd

      <script>
          function checkid() {
              $.post({
                  url: "${pageContext.request.contextPath}/check",
                  data: {'username': $("#username").val()},
                  success: function (data) {
                      if (data.toString() === 'ok') {
                          $("#userInfo").css("color", "green");
                      } else {
                          $("#userInfo").css("color", "red");
                      }
                      $("#userInfo").html(data);
                  }
              })
          }
      
          function checkpwd() {
              $.post({
                  url: "${pageContext.request.contextPath}/check",
                  data: {'password': $("#password").val()},
                  success: function (data) {
                      if (data.toString() === 'ok') {
                          $("#pwdInfo").css("color", "green");
                      } else {
                          $("#pwdInfo").css("color", "red");
                      }
                      $("#pwdInfo").html(data);
                  }
              })
          }
      </script>
      
    4. 页面效果

  • 相关阅读:
    Elastic Search(一)
    嵌入式jetty
    mybatis中的#{}和${}的区别
    拦截器和过滤器的区别
    springboot对拦截器的支持
    Springboot对filter的使用
    springboot对监听器Listener的使用
    随机数的基本概念
    hashset和treeset区别
    java中常见的api方法记录
  • 原文地址:https://www.cnblogs.com/pinked/p/12233579.html
Copyright © 2011-2022 走看看