我正在加载一些带有以下代码的jQuery的片段

var box = $(this).closest(".product-contain").children(".title:first");
box.load(url + " .maintitle", data);
var box = $(this).closest(".product-contain").children(".detail:first");
box.load(url + " .alldetail", data);

(我不想一次加载整个产品,因为它的一部分是由用户同时编辑的)

但是它向服务器提供了2个请求,以获取相同内容。

我可以以一个请求来以某种方式执行此操作吗?

谢谢!

有帮助吗?

解决方案

你可以做一个 $.get() 并加载内容自己所在的位置:这样:

 var pc = $(this).closest(".product-contain");
 $.get(url, data, function(html) {
   var resp = $(html);
   pc.children(".title:first").html(resp.find(".maintitle"));
   pc.children(".detail:first").html(resp.find(".alldetail"));
 });

这看起来有些怪异,但是 .html() 用jQuery对象是一个快捷方式 .empty().append().

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top