简体   繁体   English

Mootools在ajax请求后附加html

[英]Mootools appending html after an ajax request

I have an ajax call that looks like this, 我有一个像这样的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();
 });

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. 本质上发生的事情取决于从另一个文件返回的某些HTML的$('campaignType')值,但是我似乎无法将HTML追加到我的容器中。 Any one care to give me some advice? 有人愿意给我一些建议吗?

Thanks 谢谢

Dimitar's solution is close but is a bad solution as it recreates the whole element contents and destroys attached event handlers. Dimitar的解决方案很接近,但它是一个糟糕的解决方案,因为它会重新创建整个元素的内容并破坏附加的事件处理程序。 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. 这实际上是Request.HTML内部执行的操作。

.append is not a valid element prototype in mootools. 在mootools中,.append不是有效的元素原型。

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): 如果您想将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);
    }
});

or in your onComplete do: 或在您的onComplete中执行以下操作:

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

have fun :) 玩得开心 :)

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

If you're using mootools 1.2.4 you can change Request to Request.HTML and use append option. 如果您使用的是mootools 1.2.4,则可以将Request更改为Request.HTML并使用append选项。 (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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM