I know this has been asked many times but i'm yet to find a clear and helpful answer so I thought i'd write the question again in a clearer format?
The problem is when you use the .load() function and you have javascript that needs to run in the loaded file it doesn't work as I'm guessing the DOM has already loaded.
The easy way around this is to just load the javascript again inside the loaded file but If you have a lot of files that get loaded having javascript all over the place isn't very good practice.
Does any one know of a good way of doing this?
This is as close as I could get it to being reasonably tidy??
<a data-toggle="modal" data-target="#DIV" href="./ajax/file.php?id='.$row['id'].'"></a>
$("a[data-toggle='modal']").on('click', function() {
var target, url;
target = $(this).attr('data-target');
url = $(this).attr('href');
return $(target).load(url, function(responseText, statusText, xhr){
if(statusText == "success"){
// Re-initiate all required javascript if the load was successful.
$(".datepicker").datepicker({
changeMonth: true,
changeYear: true,
showOn: "both",
buttonImage: "./assets/img/calendar.gif",
buttonImageOnly: true,
dateFormat: 'dd-mm-yy'
});
}
if(statusText == "error"){
alert("There was a problem trying to load the page.");
}
});
});
Inside the file.php is simply a form that has fields that require things like datepicker etc.
The load is working fine.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…