如何填写电子邮件字段时,如何使提交按钮工作?

时间:2016-03-27 19:59:24

标签: javascript php jquery bootstrap-modal

我有订阅表格的模态窗口。

如何填写输入字段是否已填写,如果已填写,请使用php发送信息,否则在<p class="alert-message"></p>显示消息?

即使输入字段为空,也会在点击提交后关闭窗口。

<div class="modal fade" id="subscription">
  <div class="modal-dialog modal-dialog-subscription">  
    <div class="modal-content modal-content-subscription">    
      <div class="modal-body modal-body-subscription row">
        <div class="col-md-7">
          <img src="/image/subs1.jpg" width="100%">
        </div>
        <div class="col-md-5">
          <h3 class="h3-subscription">Pls subsribe!</h3>
          <p class="p-subscription">Sample text</p>
          <p class="p-subscription">Sample text:</p>
          <form>
            <div class="form-group">
              <input type="email" class="form-control input-email" name="input-email" id="input-email" required="required">
              <p class="alert-message"></p>
            </div>
            <button class="btn btn-shopping-cart pull-right" id="subscribe_button"><span>Subsribe</span></button>
            <button class="button btn btn-theme-default pull-left" data-dismiss="modal">Cancel</button>
          </form>
        </div>
      </div>
    </div>  
  </div>
</div>

仅适用于新访问者(Cookie为1天)

<script type="text/javascript">
$(window).load(function(){
        if ( ! $.cookie('cookieExist') ) {
        $.cookie('cookieExist', true, { expires: 1 });
        setTimeout(function(){
            $('#subscription').modal('show');
            },5000);
        }
});    
</script>

<script>
  $(document).ready(function(){
    $("#subscribe_button").click(function() {
            var email = $(".input-email").val();  

            $.ajax({
                type: "POST", 
                url: "/subscribe.php",
                //data: form_data,
                data: {email: email},
                success: function() {
                    $('.modal-body-subscription').html("<div class='successfully-subscribed' style='margin-bottom: 15px;'>");
                    $('.modal-body-subscription > .successfully-subscribed').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true' style='font-family: Geneva, Arial, Helvetica, sans-serif;'>&times;")
                    .append( "</button>");
                    $('.modal-body-subscription > .successfully-subscribed').append("<h1 class='text-center'>Thanks</h1>");
                    $('.modal-body-subscription > .successfully-subscribed').append('</div>');
                                            },
                error: function() {     
                    // Fail message
                    $('.modal-body-subscription').html("<div class='not-subscribed'>");
                    $('.modal-body-subscription > .not-subscribed').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true' style='font-family: Geneva, Arial, Helvetica, sans-serif;'>&times;").append( "</button>");
                    $('.modal-body-subscription > .not-subscribed').append("<h1>Oops!</h1>");
                    $('.modal-body-subscription > .not-subscribed').append('</div>');
                }   
            });
    });
});    


</script>

3 个答案:

答案 0 :(得分:0)

这只适用于html5。

所以这是在HTML5中添加的

 <input type="email" class="form-control input-email" name="input-email" id="input-email" required="required">

请检查您的浏览器是否可以使用HTML5,如果是,那么您的表单将不会被提交,因为它是您表单中的必填字段

答案 1 :(得分:0)

尝试以下

if (!email) {
  // then email value is empty, display error message
  $(".alert-message").show(); // if is hidden
  return false;
} else {
  // send data to server
}

答案 2 :(得分:0)

首先检查表单是否为空,如果没有继续使用ajax,否则将错误信息发布到表单中。

要存档,请创建一个函数,以便在输入为空时进行验证。我正在修改你的代码

 $(document).ready(function () {
                $("#subscribe_button").click(function () {
                    $(this).button('loading');
                    var email = $(".input-email").val();

                    /**
                     * Validate the form
                     */
                    if (email === '' || !email) {
                        $('.alert-message').append('Email is required').show();
                    } else {

                        $.ajax({
                            type: "POST",
                            url: "/subscribe.php",
                            //data: form_data,
                            data: {email: email},
                            success: function () {
                                $('.modal-body-subscription').html("<div class='successfully-subscribed' style='margin-bottom: 15px;'>");
                                $('.modal-body-subscription > .successfully-subscribed').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true' style='font-family: Geneva, Arial, Helvetica, sans-serif;'>&times;")
                                        .append("</button>");
                                $('.modal-body-subscription > .successfully-subscribed').append("<h1 class='text-center'>Thanks</h1>");
                                $('.modal-body-subscription > .successfully-subscribed').append('</div>');
                            },
                            error: function () {
                                // Fail message
                                $('.modal-body-subscription').html("<div class='not-subscribed'>");
                                $('.modal-body-subscription > .not-subscribed').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true' style='font-family: Geneva, Arial, Helvetica, sans-serif;'>&times;").append("</button>");
                                $('.modal-body-subscription > .not-subscribed').append("<h1>Oops!</h1>");
                                $('.modal-body-subscription > .not-subscribed').append('</div>');
                            }
                        });
                    }
                });
            });