Forms jQuery-刷新DIV的内容
我在网页上的jQuery弹出窗口中有一个表单。jQuery弹出窗口是一个名为.vote表单的div,其中的表单名为表单 提交表单时,jQuery弹出窗口中的内容将更改为成功消息。我需要这样做,当jQuery弹出窗口关闭时,成功消息被删除,表单被刷新回原始表单,这样当jQuery弹出窗口再次打开时,表单再次显示,而不是成功消息 我试图得到这个结果的微弱尝试涉及在jQuery弹出窗口关闭时刷新整个页面。这在一定程度上达到了预期的效果,但当页面刷新时,大多数浏览器都会弹出一个窗口,询问用户是否希望重新提交表单内容。我需要避免这种情况 这是我处理结束投票表的代码。投票表:Forms jQuery-刷新DIV的内容,forms,jquery,jquery-selectors,Forms,Jquery,Jquery Selectors,我在网页上的jQuery弹出窗口中有一个表单。jQuery弹出窗口是一个名为.vote表单的div,其中的表单名为表单 提交表单时,jQuery弹出窗口中的内容将更改为成功消息。我需要这样做,当jQuery弹出窗口关闭时,成功消息被删除,表单被刷新回原始表单,这样当jQuery弹出窗口再次打开时,表单再次显示,而不是成功消息 我试图得到这个结果的微弱尝试涉及在jQuery弹出窗口关闭时刷新整个页面。这在一定程度上达到了预期的效果,但当页面刷新时,大多数浏览器都会弹出一个窗口,询问用户是否希望重新
$('.vote-form-close').click(function(event) {
event.stopPropagation();
$(".vote-form").fadeOut("normal");
$("#the-lights").fadeTo("slow",0);
$("#the-lights").css({'display' : 'none'});
window.location.reload();
});
我怀疑它可能只刷新div,而不是整个页面,但我不知道如何完成它
有人能帮我吗
编辑:根据下面的一个答案,我修改了我的代码。我还想展示用于打开表单的代码:
$('.vote').click(function() {
$(this).parent().find(".vote-form").fadeIn("normal");
$("#the-lights").css({'display' : 'block'});
$("#the-lights").fadeTo("slow",0.7);
});
$('.vote-form-close').click(function(event) {
event.stopPropagation();
$(".vote-form").fadeOut("normal");
$("#the-lights").fadeTo("slow",0);
$("#the-lights").css({'display' : 'none'});
$(".vote-form").load(window.location.href + " .vote-form-container");
});
问题是,我在页面上有3张表格。加载投票表单容器时,它会将所有三个表单加载到.vote表单框中-如何修改代码以仅加载作为特定.vote表单一部分的.vote表单容器-我怀疑我必须使用$this,但我尝试将代码修改为此,但它不起作用:
$(".vote-form")(this).load(window.location.href + " .vote-form-container");
我想我做错了
编辑2:现在第一次重新加载表单后关闭按钮不起作用:
$('.vote').click(function() {
$(this).parent().find(".vote-form").fadeIn("normal");
$("#the-lights").css({'display' : 'block'});
$("#the-lights").fadeTo("slow",0.7);
});
$('.vote-form-close').click(function(event) {
event.stopPropagation();
$(".vote-form").fadeOut("normal");
$("#the-lights").fadeTo("slow",0);
$("#the-lights").css({'display' : 'none'});
var current_form = $(this).closest('.vote-form'),
index = $('.vote-form').index(current_form)
current_form.load(window.location.href + " .vote-form-container:eq(" + index + ")");
});
不要重新加载页面,而是重定向用户:
window.location.href = window.location.href.toString()
或者使用ajax加载新表单:
$(".vote-form").load(window.location.href + " .vote-form");
有关ajax方法的更多信息,请参见
更新:
使用jQuery的索引函数,您只能替换当前表单
// I asume your button is in the form
var current_form = $(this).closest('.vote-form'),
index = $('.vote-form').index(current_form)
current_form.load(window.location.href + " .vote-form:eq(" + index + ")");
。。。我需要这样做,当jQuery弹出窗口关闭时,成功消息被删除,表单被刷新回原始表单
因此,如果我能很好地理解:
$('.vote-form-close').click(function(event) {
event.stopPropagation();
var vf = $(".vote-form");
/* fadeout and remove inner content of the popup */
vf.fadeOut("normal", function() { vf.empty(); });
/* reset the form */
document.getElementById('form').reset();
...
});
但是,如果页面上有多个表单实例,这段代码会有帮助吗?我想可以查看我对原始帖子的编辑,但是否可以使用$this来选择正确的实例?我在上面扩展了我的帖子,请看一看,它可以工作,但是,当表单重新加载时,它会停止关闭按钮的工作。请参阅上面我的更新代码。事实上,它似乎打破了任何应该在表单使用ajax提交的框内运行的脚本