Mootools 在 ajax 请求后附加 html

发布于 2024-09-07 15:25:46 字数 538 浏览 6 评论 0原文

我有一个看起来像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

咽泪装欢 2024-09-14 15:25:46

Dimitar 的解决方案很接近,但却是一个糟糕的解决方案,因为它重新创建了整个元素内容并破坏了附加的事件处理程序。更好的解决方案是:

Element.implement({
    append: function(newhtml) {
        return this.adopt(new Element('div', {html: newhtml}).getChildren());
    }
});

这实际上是 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:

Element.implement({
    append: function(newhtml) {
        return this.adopt(new Element('div', {html: newhtml}).getChildren());
    }
});

this is actually what Request.HTML internally does.

如痴如狂 2024-09-14 15:25:46

.append 不是 mootools 中的有效元素原型。

如果你想将 html 附加到现有的 HTML 中,那么你可以通过在你的站点 lib/core 位中定义来使 .append 有效(如果你经常使用它,我会考虑这一点):

Element.implement({
    append: function(newhtml) {
        // silly name, does not say to me you are appending html. rename to appendHTML
        return this.set("html", this.get("html") + newhtml);
    }
});

或者在你的 onComplete do 中:

var cf = $('campaignForm');
cf.set("html", cf.get("html") + this.response.text);

玩得开心: )

.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):

Element.implement({
    append: function(newhtml) {
        // silly name, does not say to me you are appending html. rename to appendHTML
        return this.set("html", this.get("html") + newhtml);
    }
});

or in your onComplete do:

var cf = $('campaignForm');
cf.set("html", cf.get("html") + this.response.text);

have fun :)

如果您使用的是 mootools 1.2.4,您可以将 Request 更改为 Request.HTML 并使用 append 选项。 (不确定旧版本中是否有附加选项)

$('campaignType').addEvent('change', function(){
  new Request.HTML({
    method: 'get',
    url: '/admin/admin_' + $('campaignType').value + '.php',
    append: $('campaignForm')
  }).send();
});

If you're using mootools 1.2.4 you can change Request to Request.HTML and use append option. (Not sure that append option was in older versions)

$('campaignType').addEvent('change', function(){
  new Request.HTML({
    method: 'get',
    url: '/admin/admin_' + $('campaignType').value + '.php',
    append: $('campaignForm')
  }).send();
});
你在我安 2024-09-14 15:25:46

我认为您喜欢使用 onSuccess 而不是 onComplete

I think you like to use onSuccess instead of onComplete

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文