简体   繁体   English

使用JS显示/隐藏弹出窗口,窗体

[英]Show/Hide Pop-Up with JS, form

I have a form, when the user click on send..the fields are sent to AJAX and then to php function. 我有一种形式,当用户单击send..the字段发送到AJAX,然后发送到php函数。 After this, open a pop-up/div. 之后,打开一个弹出窗口/ div。

I'm trying to do, when user click somewhere on the site, this div is hide. 我正在尝试做,当用户单击网站上的某个位置时,此div隐藏。 But isn't working, any ideas? 但是行不通,有什么主意吗?

HTML Form: HTML表单:

<form id="contactForm">
                <select id="contactFormSelect">
                  <option value="General" selected>General</option>
                  <option value="Support">Support</option>
                  <option value="Suggestions">Suggestions</option>
                  <option value="Jobs">Jobs</option>
                  <option value="Press">Press</option>
                  <option value="Founder">Founder</option>
                </select>
                <br />
                <input id="contactForm" type="text" name="name" placeholder="Your name"><br />
                <input id="contactForm" type="text" name="email" placeholder="Your email"><br />
                <input id="contactForm" type="text" name="subject" placeholder="Subject"><br />
                <textarea id="write" name="textarea" placeholder="Write here"></textarea><br />
                <input id="submitForm" type="button" value="SEND" onclick="contactRequest(this);">
            </form>

HTML Pop-up/Div: HTML弹出/分区:

<div id="contactSent">
<div id="backgroundOpacity"></div>
<div class="contactSent">
    <div id="contactSentFirst">
        Thank you!
    </div>
    <div id="contactSentSecond">
        Your message has been successfully sent. We will contact you very soon!
    </div>
</div>
<div id="buttonOK">
    Ok
</div>
</div>  

JS: JS:

function contactRequest(elem)
{   
var postData = $('#contactForm').serialize();
    postData += "&textarea="+$('#write').val();
    postData += "&selectedOption="+$('#contactFormSelect').val();;

if($(elem).parent().children('#contactForm').val() != ''){      
    $.ajax(
    {
          url: "/newcontact/pedido-contato/",
          type: "POST",
          data: postData
    });

}
$('#contactSent').show();
}


$('body').click(function(){
$('#contactSent').hide();
});

The pop-up div element you're talking about sounds like the bootstrap modal element. 您正在谈论的弹出div元素听起来像bootstrap modal元素。 Perhaps you should take a look here: http://getbootstrap.com/javascript/#modals . 也许您应该在这里看看: http : //getbootstrap.com/javascript/#modals Modals are really easy to implement and they give documentation for it on the site. 模态确实易于实现,并且可以在网站上提供模态文档。 Hope this helps 希望这可以帮助

It seems that your backgroundOpacity element is on top of the body tag. 看来您的backgroundOpacity元素在body标签之上。 Try to assign the event to it. 尝试为其分配事件。

$('#backgroundOpacity').click(function(){
    $('#contactSent').hide();
});

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

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