在JavaScript代码满意之前阻止表单发布 [英] prevent form from POSTing until javascript code is satisfied

查看:87
本文介绍了在JavaScript代码满意之前阻止表单发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我点击提交按钮,它都会触发 alert(空用户名),但是会引导我前往 checklogin.php code> ...我怎样才能避免这种情况,并让它保持在页面上,除非字段不为空?

whenever i click on submit button it fires alert("empty username") but goes a head and directs me to checklogin.php...how can i avoid that and let it remain on the page unless the field is not empty?

</body>
</html>
<script type="text/javascript"></script>       
function RequiredFields(){
     var username=document.forms["login"]["username"].value;


     if (username==""||username==null){
         alert("empty username")
         document.login.username.focus();
         if(document.all||document.getElementById){
             return false;
         }

     }
}
</script>
<form action="checklogin.php" method="POST" onSubmit="RequiredFields()">
Username<input type="text" name="username"/>
Password<input type="password" name="password"/>
<input type="submit" value="Login"  />
</form>
</body>
</html>


推荐答案

使用此代码

Use this code

<input type="submit" value="Login" onclick="return RequiredFields()" />

这篇关于在JavaScript代码满意之前阻止表单发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆