简体   繁体   中英

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. After this, open a pop-up/div.

I'm trying to do, when user click somewhere on the site, this div is hide. But isn't working, any ideas?

HTML Form:

<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:

<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:

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. Perhaps you should take a look here: 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. Try to assign the event to it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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