新建用户名重复ajax回调提示 提交按钮失效
- 输入框
<input type="text" class="form-control" name="username" id="username" onchange="isExistName(this)" placeholder="用户名称" value="">
- 按钮
<button type="submit" class="btn bg-maroon" id="savebtn">保存</button>
- js
function isExistName(a){ var username = $("#username").val(); alert(username); $.ajax({ url: "${pageContext.request.contextPath}/user/isExistName", type:"post", data:{"username":username}, success:function (data) { if (data === "true") { alert(data); $("#username").attr("style", "border:1px solid red"); $("#savebtn").prop("disabled", true); }else{ alert(data); $("#username").attr("style", ""); $("#savebtn").prop("disabled", false); } } }) }
- controller
@RequestMapping("/isExistName") @ResponseBody public String isExistName(String username) { System.out.println("username:"+username); SysUser user = sysUserService.findByName(username); Boolean flag = false; if (user != null) { flag = true; } return String.valueOf(flag); }