通过使用form的onsibmit来控制是否提交数据
返回值为真是提交,其他不变,示例如下:
JS部分
function check() { var newPwd = document.getElementById("newPwd"); var confirmNewPwd = document.getElementById("confirmNewPwd"); if (newPwd.value == confirmNewPwd.value) return true; else { var err = document.getElementById("errMSG"); err.style.display = ""; return false; } }
<form action="/student/UpdatePwd" onsubmit="return check()"> <div class="control-group"> <label class="control-label" for="basicinput">旧密码</label> <div class="controls"> <input placeholder="请输入…" data-original-title="" class="span8 tip" type="password" name="oldPwd" id="oldPwd"> </div> </div> <div class="control-group"> <label class="control-label" for="basicinput">新密码</label> <div class="controls"> <input placeholder="请输入…" data-original-title="" class="span8 tip" type="password" name="newPwd" id="newPwd"> <span class="help-inline" style="color:red;display:none" id="errMSG">两次密码不一致</span> </div> </div> <div class="control-group"> <label class="control-label" for="basicinput">再次输入新密码</label> <div class="controls"> <input placeholder="请输入…" data-original-title="" class="span8 tip" type="password" id="confirmNewPwd"> </div> </div> <div class="control-group"> <div class="controls "> <button type="submit" class="btn-inverse btn-large">更新</button> </div> </div> </form>