当页面最初加载时,我使用下面的javascript代码来加载recrefresh.php。当执行函数ajaxvote时,我希望刷新以下div:
<div class="recrefresh"></div>
成功后我尝试添加以下内容:,但执行ajaxvote后div不会刷新:
$.get('recrefresh.php', function(data) {
$('.recrefresh').html(data);
});
整个脚本:
<div class="recrefresh"></div>
<script type="text/javascript">
$.get('recrefresh.php', function(data) {
$('.recrefresh').html(data);
});
</script>
<script>
$('.ajaxvote').click(function(e){
e.preventDefault(); // Prevents default link action
$.ajax({
url: $(this).attr('href'),
success: function(data){
alert("Vote Saved.");
$.get('recrefresh.php', function(data) {
$('.recrefresh').html(data);
});
}
});
});
</script>
使用此
<script>
$(document).ready(function(){
//run function here
refreshMyDiv();
//click event for dynamically generated elements
$('.recrefresh').on('click' , '.ajaxvote' , function(){
//run function inside click
refreshMyDiv();
});
});
// function to refresh the .recrefresh div
function refreshMyDiv(){
$.get('recrefresh.php', function(data) {
$('.recrefresh').html(data);
});
}
</script>
为了解决这个问题,将父div带到<div class="recrefresh"></div>
,因为直接div有时不加载,所以用其他div
包装这个div
,比如说
<div class="recrefresh_parent"><div class="recrefresh"></div></div>
并使用CCD_ 4刷新表。有一种方法可以在ajax调用后刷新或加载div:
$('.recrefresh_parent').load("recrefresh.php .recrefresh_parent");