在提交到php之前validation表单

您好我正在实现一个登录页面,从PHP端一切正常,但现在我将使用jQuery实现一个更多的function:我想检查登录的字段是否为空,如果不是在页面上打印的东西,否则继续使用php,怎么做?

这是我的HTML代码的一部分:

将事件绑定到提交表单(注意:不要单击提交按钮)。 这是你的代码检查表单的地方,如果有必要,阻止它被发布。

 $('#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