Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  178] [ 4]  / answers: 1 / hits: 18925  / 9 Years ago, wed, january 6, 2016, 12:00:00

P.S I am not very good in javascript and I know this question has been asked but the solutions does not apply to my problem so I will ask this question again.



I'm stuck with this problem and haven't find the solution yet. I wanted to clear all the things that happened into my modal after its close.



My modal is a form modal, so whenever I click submit, codeigniter validation messages will show by the use of ajax on <div style=font-size:10px;width:50%; id=info></div> . Now whenever I close the modal, the validation messages stays when I re-open it. What should be done in order to clear those out after closing the modal?



This is the function called by the ajax:



public function addcomp () {
$this->load->library('form_validation');
$this->load->helper('security');
$this->form_validation->set_rules('comp_name','Computer Name','trim|required|callback_pc_exist');
$this->form_validation->set_rules('status','Status','trim|required');
$this->form_validation->set_rules('location','Location','trim|required');

if ($this->form_validation->run()) {

$this->load->model('asset_model');
$result=$this->asset_model->addpc();

if (!$result) {
echo mysqli_error($result);
} else {
echo <div class='alert alert-success alert-dismissable'>
<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>
Computer Successfully Added!</div>;
}
}
echo validation_errors(<div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>,</div>);
}


This is the ajax called by submission:



$(document).ready(function(){
$(#addpc).submit(function(){
var formdata=$(#addpc).serialize();
$.ajax({
url:asset_management/addcomp,
type:POST,
data: formdata,
async: true,
}).done(function( formdata ) {
$('#info').html(formdata);
$(#addpc)[0].reset();
viewdata();
});
return false;
});
});

More From » jquery

 Answers
9

You can make use of the Bootstrap's Modal Events. You should be concerned about these two:





  • hide.bs.modal This event is fired immediately when the hide instance method has been called.

  • hidden.bs.modal This event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete).




So, you can do this:



$(function () {
// Delegating to `document` just in case.
$(document).on(hidden.bs.modal, #myModalID, function () {
$(this).find(#info).html(); // Just clear the contents.
$(this).find(#info).remove(); // Remove from DOM.
});
});


Update



Seeing your code, you might need to use this:



$(function () {
// Delegating to `document` just in case.
$(document).on(hidden.bs.modal, #myModalID, function () {
$(this).find(.alert-danger).remove(); // Remove from DOM.
});
});

[#63830] Sunday, January 3, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hasancolec

Total Points: 603
Total Questions: 95
Total Answers: 98

Location: South Korea
Member since Sat, Oct 2, 2021
3 Years ago
;