javascript - 表单在弹出框中提交

标签 javascript php html forms

我在弹出框中创建了一个登录表单。当用户名字段留空时,将出现一条错误消息,通知用户填写空的用户名字段。作为测试,我单击登录按钮,保留用户名字段,消息按预期显示在弹出框中。但问题是弹出框立即关闭。 所以,我的问题是如何保持弹出框打开并显示错误消息?

这是我的脚本:

<!doctype html>
<html lang="en-US">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <title>Modal Login Window Demo</title>
  <link rel="shortcut icon" href="http://designshack.net/favicon.ico">
  <link rel="icon" href="http://designshack.net/favicon.ico">
  <link rel="stylesheet" type="text/css" media="all" href="http://designshack.net/tutorialexamples/modal-login-jquery/style.css">
  <script type="text/javascript" src="http://designshack.net/tutorialexamples/modal-login-jquery/js/jquery-1.9.1.min.js"></script>
  <script type="text/javascript" charset="utf-8" src="http://designshack.net/tutorialexamples/modal-login-jquery/js/jquery.leanModal.min.js"></script>
</head>

<body>
 <div id="w">
    <div id="content">
      <center><a href="#loginmodal" class="flatbtn" id="modaltrigger">Modal Login</a</center>
    </div>
</div>
<div id="loginmodal" style="display:none;">
<?php
if($_POST["loginbtn"]){
    if(!$_POST["username"]){
        echo "<center><font color=red>please fill your username</font></center>";
    }elseif(!$_POST["password"]){
        echo "<center><font color=red>please fill your password</font></center>";
    }
}
?>
    <h1>User Login</h1>
    <form method="post">
      <label for="username">Username:</label>
      <input type="text" name="username" id="username" class="txtfield" tabindex="1">

      <label for="password">Password:</label>
      <input type="password" name="password" id="password" class="txtfield" tabindex="2">

      <div class="center"><input type="submit" name="loginbtn" id="loginbtn" class="flatbtn-blu hidemodal" value="Log In" tabindex="3"></div>
   </form>
  </div>
<script type="text/javascript">
$(function(){
  $('#loginform').submit(function(e){
    return false;
  });

  $('#modaltrigger').leanModal({ top: 110, overlay: 0.45, closeButton: ".hidemodal" });
});
</script>
</body>
</html>

最佳答案

closeButton 选项将始终导致单击相应按钮时关闭模式。查看leanModal源代码,似乎没有任何直接的方法来操纵其事件处理回调。

因此,如果您想要做的只是在未填写字段的情况下保持表单模式打开,并让服务器端代码执行验证,您只需执行以下操作:

$('#loginform').submit(function(e){
    if(!$('#username').val()) {
        $('#loginmodal').show();
    }
    else
        console.log("Enter your username");
    return false;
});

现场演示 jsfiddle 。请注意,我向表单标记添加了一个 id,并修复了 fiddle 中的一些格式错误的 HTML 标记。

关于javascript - 表单在弹出框中提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25540705/

相关文章:

javascript - 绕过 iframe 沙箱?

用于用引号引起来的多行字符串的 Javascript 正则表达式,可能包含引号

JavaScript 对象继承

JavaScript/jQuery VIN 验证器

php - 插入时如果 "title"已经存在于表中则更新记录

css - div需要在页面的每个 Angular 落不重叠吗?

jquery - 安装 jQuery slider - 添加文本而不是图片

javascript - 需要帮助了解如何从 JS 文件格式化表数据的内联 CSS

php - Android 如何使用身份验证用户名和密码从以下 URL 从 Web 服务器获取响应

php - 为循环中的每个表行赋予唯一类