简体   繁体   English

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

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

I am trying a code where there is a form to accept email address for newsletter subscription; 我正在尝试一个代码,其中有一种形式可以接受订阅电子报的电子邮件地址;

When the alert message is displayed on empty field or invalid email submit, the alert message div is created within the form tag; 当警报消息显示在空白字段或无效的电子邮件提交中时,警报消息div将在form标签内创建; I want the message div out of the form tag. 我希望消息div超出表单标签。 Below are codes in use; 以下是正在使用的代码;

JAVASCRIPT 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 的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 快照中的代码检查快照

Use div outside of form and edit your js code like 在表格外使用div并编辑您的js代码,例如

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

Use this where u want Hope this working fine. 在您想要的地方使用它。希望它能正常工作。

You can use jQuery after() instead of append . 您可以使用jQuery after()代替append Like this: 像这样:

$("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> 

Your alert message div is now appended inside the form with the following line: 现在,您的警报消息div将在行内附加以下行:

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

You can simply create the alert div outside the form by setting the following: 您可以通过设置以下内容在表单外部简单地创建Alert div:

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

Now the alert message div is outside the form and the rest of the stuff will work as it is and the message will be shown outside the form. 现在,警报消息div位于表单外部,其余内容将按原样工作,并且消息将显示在表单外部。

尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM