gpt4 book ai didi

javascript - 成功刷新页面

转载 作者:行者123 更新时间:2023-12-03 08:07:42 24 4
gpt4 key购买 nike

我使用下面的 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>

关于javascript - 成功刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34305819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com