将事件绑定到表单的提交(注意:并非单击提交按钮)。那就是您的代码去检查表单的地方,并在必要时阻止其发布。
$('#loginForm').on('submit', function(e){ if ($('#login').val()=='' || $('#password').val()==''){ e.preventDefault(); // alert('Fill in both fields'); // You can use an alert or a dialog, but I'd go for a message on the page $('#errormsg').text('Fill in both fields').show(); }else{ // do nothing and let the form post }});如果您希望显示消息而不是使用对话框,请在页面上某个位置(最好是在“提交”按钮附近)添加errormsg div
<p id="errormsg" ></p>



