简体   繁体   English

将附加到HTML上的jQuery加载功能

[英]Jquery load function that will append onto HTML already present

I am trying to make an AJAX call which will append html ie add additional html to what is already present between the tags. 我正在尝试进行AJAX调用,该调用将追加html,即在标签之间已经存在的内容中添加其他html。 Here is what my load function looks like. 这是我的加载函数的样子。

<script>
    $(document).ready(function(){
      $(".tid-select").change(function(){
        $(".tid-list").load("/saffron_main/test");
      });
    });
</script>

How would I modify this function in order to get it to append to the class .tid-list. 我将如何修改此函数以使其附加到类.tid-list。 Thanks in advance. 提前致谢。

You have to get data via ajax and use $('.tid-list').append(data); 您必须通过ajax获取数据并使用$('.tid-list').append(data);

<script>
$(document).ready(function(){
  $(".tid-select").change(function(){
   $.get("/saffron_main/test",function(data){
    $('.tid-list').append(data);
   });
 });
});
</script>

You may also use $('.tid-list').prepend(data); 您也可以使用$('.tid-list').prepend(data); to insert before the current HTML. 在当前HTML之前插入。

Or instead of replacing the html you can just load the data into a variable and then add it to the DOM. 或代替替换html,您只需将数据加载到变量中,然后将其添加到DOM中即可。

Example: 例:

$.ajax({
    url: '/saffron_main/test',
    success: function (data) {
        $('.tid-list').append(data);
    }
});

This way events that have been bound to the existing html will stay bound. 这样,已绑定到现有html的事件将保持绑定状态。

Just change the change handler 只需更改更改处理程序

$(".tid-select").change(function(){
   $.get("/saffron_main/test", function(data){
      $(".tid-list").append(data);
   });
});

edit: yeah, the other guys are right that append makes more sense 编辑:是的,其他人是正确的,追加更有意义

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

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