如何从<form>标记中移出警报消息div

时间:2016-07-11 10:12:43

标签: javascript html forms alert message

我正在尝试一个代码,其中有一个表单接受订阅时事通讯的电子邮件地址;

当空字段或无效电子邮件提交时显示警报消息时,将在表单标记内创建警报消息div;我想从表单标签中删除消息div。以下是正在使用的代码;

JAVASCRIPT

// newsletter

    (function(){

        var subscribe = $('#subscribe');
        subscribe.append('<div class="message_container_subscribe"></div>');
        var message = $('.message_container_subscribe'),text;

        subscribe.on('submit',function(e){
            var self = $(this);

            if(self.find('input[type="email"]').val() == ''){
                text = "Please enter your e-mail!";
                message.html('<div class="alert_box color_red r_corners error"><i class="fa fa-exclamation-triangle"></i> '+text+'</div>')
                    .slideDown()
                    .delay(4000)
                    .slideUp(function(){
                        $(this).html("");
                    });

            }else{
                self.find('span.error').hide();
                $.ajax({
                    type: "POST",
                    url: "newsletter.php",
                    data: self.serialize(), 
                    success: function(data){
                        if(data == 1){
                            text = "Your email has been sent successfully!";
                            message.html('<div class="alert_box r_corners color_green success"><i class="fa fa-check-square"></i> '+text+'</div>')
                                .slideDown()
                                .delay(4000)
                                .slideUp(function(){
                                    $(this).html("");
                                })
                                .prevAll('input[type="email"]').val("");
                        }else{
                            text = "Invalid email address!";
                            message.html('<div class="alert_box color_red r_corners error"><i class="fa fa-exclamation-triangle"></i><p>'+text+'</p></div>')
                                .slideDown()
                                .delay(4000)
                                .slideUp(function(){
                                    $(this).html("");
                                });
                        }
                    }
                });
            }
            e.preventDefault();
        });

    })();

HTML

<form action="newsletter.php" method="post" name="subscribe-form" id="subscribe" class="subscribe-form">
                    <input type="email" placeholder="Your email address" id="emailnews" class="email" name="emailnews">
                    <button type="submit" value="Submit" class="submit">Subscribe</button>
                </form>

SNAPSHOT FROM CODE INSPECT IN CHROME

4 个答案:

答案 0 :(得分:0)

在表单之外使用div并编辑你的js代码,如

(".message_container_subscribe").html("Your Message Here")

在你想要的地方使用它 希望这个工作正常。

答案 1 :(得分:0)

您可以使用jQuery after()代替append。像这样:

$("form").after('<div class="message_container_subscribe"></div>');

&#13;
&#13;
// newsletter

    (function(){

        var subscribe = $('#subscribe');
        $("form").after('<div class="message_container_subscribe"></div>');
        var message = $('.message_container_subscribe'),text;

        subscribe.on('submit',function(e){
            var self = $(this);

            if(self.find('input[type="email"]').val() == ''){
                text = "Please enter your e-mail!";
                message.html('<div class="alert_box color_red r_corners error"><i class="fa fa-exclamation-triangle"></i> '+text+'</div>')
                    .slideDown()
                    .delay(140000)
                    .slideUp(function(){
                        $(this).html("");
                    });

            }else{
                self.find('span.error').hide();
                $.ajax({
                    type: "POST",
                    url: "newsletter.php",
                    data: self.serialize(), 
                    success: function(data){
                        if(data == 1){
                            text = "Your email has been sent successfully!";
                            message.html('<div class="alert_box r_corners color_green success"><i class="fa fa-check-square"></i> '+text+'</div>')
                                .slideDown()
                                .delay(4000)
                                .slideUp(function(){
                                    $(this).html("");
                                })
                                .prevAll('input[type="email"]').val("");
                        }else{
                            text = "Invalid email address!";
                            message.html('<div class="alert_box color_red r_corners error"><i class="fa fa-exclamation-triangle"></i><p>'+text+'</p></div>')
                                .slideDown()
                                .delay(4000)
                                .slideUp(function(){
                                    $(this).html("");
                                });
                        }
                    }
                });
            }
            e.preventDefault();
        });

    })();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="newsletter.php" method="post" name="subscribe-form" id="subscribe" class="subscribe-form">
                    <input type="email" placeholder="Your email address" id="emailnews" class="email" name="emailnews">
                    <button type="submit" value="Submit" class="submit">Subscribe</button>
                </form>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您的警报消息div现在附加在表单内,并带有以下行:

subscribe.append('<div class="message_container_subscribe"></div>');

您可以通过设置以下内容在表单外部创建警报div:

 subscribe.after('<div class="message_container_subscribe"></div>');

现在警告消息div在表单之外,其余部分将按原样运行,消息将显示在表单之外。

答案 3 :(得分:0)

试试这个

$('<div class="alert_box color_red r_corners error"><i class="fa fa-exclamation-triangle"></i><p>'+text+'</p></div>').insertAfter($('#subscribe'));