AJAX 调用后 jQuery 重新绑定 .submit()
我正在创建一个论坛式系统,在其中创建帖子,然后用户可以对原始帖子发表评论。 (我所做的实际上在上下文中非常不同,但主题是相同的)。
添加评论的表单是帖子的一部分,它是使用 jQuery $.get() 拉入的。
这是当前的提交处理程序:
$('#add-stage-form').submit(function(){
var data = $('#add-stage-form').serialize();
add_stage(data);
return false;
});
好的..现在,我需要在拉入后重新绑定它,就像可以使用 live()
一样。
拜托,如果有人建议 live('submit', function(){ ... })
我会尖叫,因为提交不像点击那样工作!
我也很清楚,我可以在同一个按钮上绑定“click”事件 - 如果可能的话,我更宁愿绑定 .submit() 事件,
提前致谢! :)
I'm creating a forum-style system where posts are created and users can then post comments on the original post. (What I'm doing is actually very different in context but the theme is the same).
The form to add a comment is part of the Post, which is pulled in using jQuery $.get().
Here's the submit handler currently:
$('#add-stage-form').submit(function(){
var data = $('#add-stage-form').serialize();
add_stage(data);
return false;
});
Okay.. now, I need that to be re-bound once it's pulled in, in the same was live()
can be used.
And please, if anybody suggests live('submit', function(){ ... })
I WILL scream because submit doesn't work like click!
I am also well aware that you I could bind the 'click' event on the same button - I'd much rather bind the .submit() event though, if possible
Thanks in advance! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 get 方法的 onComplete 回调中手动添加绑定...
*编辑
将 add-stage-form 更改为类而不是 id。您提到每个帖子都会有一个表格?
You can add the binding manually in the onComplete callback of the get method...
*edit
Changed add-stage-form to be a class rather than an id. You mentioned there will be a form per post?
您尝试过使用
Live()
吗? :)但说真的,如果您将单击事件附加到按钮,然后调用
jQuery.Post()
操作,这可能不是一个坏主意。或者甚至是提交页面的 javascript 方法。我知道这不是(最好的)解决方案,但它可能会让你越线。
如果有更好的答案出现,我会撤回这个答案。
Have you tried using
Live()
? :)But seriously, it might not be such a bad idea if you attach a click event to the button which then calls a
jQuery.Post()
opperation. Or even a javascript method that submits the page.I know this is not the (best) solution but it might get you over the line.
I'll retract this answer if a better one comes along.