效果图
1、表sys_student中以注册用户:JieFighting
2、添加学生页面
3、添加已经注册的【学生名称】,该输入框失去焦点后出现提示信
4、添加未注册的学生名称,输入框失去焦点后无提示信息
2、validate基础使用
引入jquery.min.js(网上很容易找到)
3、代码部分
html
<form class="form-horizontal m" id="form-student-add">
<div class="form-group">
<label class="col-sm-3 control-label">学生名称:</label>
<div class="col-sm-8">
<input name="studentName" id="studentName" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">年龄:</label>
<div class="col-sm-8">
<input name="studentAge" id="studentAge" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">爱好:</label>
<div class="col-sm-8">
<input name="studentHobby" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">性别:</label>
<div class="col-sm-8">
<select name="studentSex" class="form-control m-b" th:with="type=${@dict.getType('sys_student_sex')}">
<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">状态:</label>
<div class="col-sm-8">
<div class="radio-box" th:each="dict : ${@dict.getType('sys_student_status')}">
<input type="radio" th:id="${'studentStatus_' + dict.dictCode}" name="studentStatus" th:value="${dict.dictValue}" th:checked="${dict.default}">
<label th:for="${'studentStatus_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">生日:</label>
<div class="col-sm-8">
<div class="input-group date">
<input name="studentBirthday" class="form-control" placeholder="yyyy-MM-dd" type="text">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
</div>
</div>
</div>
</form>
js
$("#form-student-add").validate({
onkeyup: false,
rules: {
studentName: {
minlength: 2,
maxlength: 20,
remote: {
url: prefix + "/checkStudentNameUnique",
type: "post",
dataType: "json",
data: {
"studentName": function () {
return $.common.trim($("#studentName").val());
}
},
dataFilter: function (data, type) {
return $.validate.unique(data);
}
}
},
studentAge:{
required:true,
min:10,
max:100
}
},
messages: {
"studentName": {
remote: "用户已经存在!",
min:"用户年龄需要超过10岁",
max:"用户年龄需要小于100岁"
},
"studentAge": {
required: "请输入年龄"
}
},
focusCleanup: true
});
mapper
<select id="checkStudentNameUnique" parameterType="String" resultType="int">
select count(1) from sys_student where student_name=#{studentName} limit 1
</select>
注:studentName的remote提示信息是根据控制器返回的值来决定是否显示。如果控制器返回的值为1则提示,0则不提示