为了账号安全,请及时绑定邮箱和手机立即绑定

ajax校验数据库数据是否存在

ajax校验数据库数据是否存在

慕工程0101907 2019-04-24 18:19:15
<!-- 查询student_id是否存在 -->     <select id="getStudent_id" resultMap="studentMap" parameterType="int">          SELECT * from student where student_id = #{student_id}     </select>@RequestMapping("/studentID")     public Student getStudent_id(Integer student_id) {         Student student = userService.getStudent_id(student_id);         if (student==null){             System.out.println("没有");         }else{             System.out.println("有");         }         return student;     }function check() {//js表单验证方法         var text = document.getElementById("xh").value;//通过id获取需要验证的表单元素的值         if (text == "") {             alert("请输入学号!");//弹出提示             return false;//返回false(不提交表单)         }         if (!(/(^[1-9]\d*$)/.test(text))) {             alert("请输入学号!");//弹出提示             return false;//返回false(不提交表单)         }         var student_id=$("#student_id").val();         $.ajax({            url:"<%=basePath%>user/studentID",            type:"post",            data:{"student_id":student_id},            dataType:"json",           success:function(data){                if (data!=null ) {                    alert("有");             }else if(data==null && username !=''){                 alert("没有");             }                xmlhttp.open("GET","getcustomer.asp?q="+str,true);                xmlhttp.send();                //alert("啊啊啊");           }         });         return true;//提交表单     } </script> <body>     <div class="listDIV">         <table border="1" width="50%" height="50%" style="text-align: center;">             <form action="<%=basePath%>user/one" method="post">                 <input type="text" id="xh" name="student_id" placeholder="根据学号查询">                 <button class="glyphicon glyphicon-select" onclick="return check();">查询</button>             </form>我用ssm做的增删改查,搜索我想先校验一下数据库数据是否存在,存在就直接跳转查询,不存在就弹窗提示,中止提交表单,JS代码是我粘过来的,不会JS,有错误希望能告诉我如何改正,谢谢
查看完整描述

3 回答

?
一只斗牛犬

TA贡献1784条经验 获得超2个赞

  1. form表单

<form action="<%=basePath%>user/one" method="post" onsubmit="return check(this)">
        <input type="text" id="xh" name="student_id" placeholder="根据学号查询">
        <button class="glyphicon glyphicon-select">查询</button>
</form>
  1. js表单校验

function check(form) {
    if (form.student_id.value == '') { 
        alert("查无此人"); 
        form.student_id.focus();
        return false;
    }
    return true;
}
  1. java后端

@Controller
@RequestMapping("/user")
public class Xxx{

    @RequestMapping("/one")
  public Student getStudent_id(Integer student_id) {

    }
}


查看完整回答
反对 回复 2019-05-13
  • 3 回答
  • 0 关注
  • 911 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号