如何将警告消息 div 移出 <form> 标签

How can I move alert message div out of <form> tag

我正在尝试一个代码,其中有一个表单可以接受电子报订阅的电子邮件地址;

当警告消息显示在空白字段或无效电子邮件提交时,警告消息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

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

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

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

您可以使用 jQuery after() 而不是 append。像这样:

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

// 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();
        });

    })();
<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>

您的提醒消息 div 现在已附加到表单中,并包含以下行:

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

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

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

现在警告消息 div 位于表单之外,其余内容将按原样工作,消息将显示在表单之外。

试试这个

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