简体   繁体   中英

Hide modal on form submit

I have the following function in my website to submit a form thats in a modal. All works fine except when i uncomment that commented line, when I uncomment that line my test mysql insert on page /index.php/trainings/add isn't executed. I'm new to javascript and ajax so I fear I'm missing something stupid. The modal div I want to close is called id="addModal", it tried simply $(div#addModal).hide(); but this only hides the div and leaves the black "matt" background over the webpage.

$(function() {  
  $("button#submit_button").click(function() {  
    var date = $("input#date").val();  
    var dataString = 'date='+ date;
    $.ajax({  
        type: "POST",  
        url: "/index.php/trainings/add",  
        data: dataString,  
        success: function() {  
            //$(div#addModal).modal.hide();
        }  
    });
    return false;
  });  
});    

would be very gratefull if someone could help.

Thanks, James

EDIT:

Don't know if this could be of any use but this is my modal with the form in it:

<div id="addModal" class="modal hide faid" area-hidden="true">
    <div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h3>Add Training</h3>
</div>
<form class="form-horizontal" id="addform">
    <div class="control-group">
    <label class="control-label">Date</label>
            <div id="datetimepicker1" class="input-append date">
        <input data-format="dd/MM/yyyy hh:mm:ss" type="text" id="date"></input>
        <span class="add-on">
        <i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
        </span>
        </div>
    </div>
    <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button type="submit" id="submit_button" class="btn btn-primary">Save changes</button>
    </div>
</form>
</div>  

Trying referencing the modal div itself. And check the bootstrap documentation for more

$("#addModal").modal('hide');

this should work, as long as your div id is "addModal"

...
success: function() {  
  $('#addModal').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