Mootools 在 ajax 请求后附加 html
我有一个看起来像这样的 ajax 调用,
$('campaignType').addEvent('change', function(){
alert($('campaignType').value);
var request = new Request({
method: 'get',
url: '/admin/admin_' + $('campaignType').value + '.php',
onRequest:function() {
alert('Request has been made, please be patient')
},
onComplete:function(response) {
$('campaignForm').append(response);
}
}).send();
});
本质上发生的情况取决于从另一个文件返回某些 HTML 的 `$('campaignType') 的值,但是我似乎无法将 HTML 附加到我的容器中。有人愿意给我一些建议吗?
谢谢
I have an ajax call that looks like this,
$('campaignType').addEvent('change', function(){
alert($('campaignType').value);
var request = new Request({
method: 'get',
url: '/admin/admin_' + $('campaignType').value + '.php',
onRequest:function() {
alert('Request has been made, please be patient')
},
onComplete:function(response) {
$('campaignForm').append(response);
}
}).send();
});
Essentially what happens is depending on what the value of `$('campaignType') some HTML is returned from another file, however I cannot seem to get the HTML to append on to my container. Any one care to give me some advice?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Dimitar 的解决方案很接近,但却是一个糟糕的解决方案,因为它重新创建了整个元素内容并破坏了附加的事件处理程序。更好的解决方案是:
这实际上是 Request.HTML 内部所做的。
Dimitar's solution is close but is a bad solution as it recreates the whole element contents and destroys attached event handlers. A better solution would be:
this is actually what Request.HTML internally does.
.append 不是 mootools 中的有效元素原型。
如果你想将 html 附加到现有的 HTML 中,那么你可以通过在你的站点 lib/core 位中定义来使 .append 有效(如果你经常使用它,我会考虑这一点):
或者在你的 onComplete do 中:
玩得开心: )
.append is not a valid element prototype in mootools.
if you want to append html to an existing one, then you can either MAKE .append valid by defining in your site lib/core bit (I would consider this if you use it a lot):
or in your onComplete do:
have fun :)
如果您使用的是 mootools 1.2.4,您可以将
Request
更改为Request.HTML
并使用append
选项。 (不确定旧版本中是否有附加选项)If you're using mootools 1.2.4 you can change
Request
toRequest.HTML
and useappend
option. (Not sure that append option was in older versions)我认为您喜欢使用
onSuccess
而不是onComplete
I think you like to use
onSuccess
instead ofonComplete