简体   繁体   English

仅替换jquery ajax调用中的片段

[英]Only replace fragment in jquery ajax call

I'm trying to basically mimic the functionality of load with an ajax request. 我试图从根本上模仿ajax请求的加载功能。 The reason for this is I need to have access to the beforeSend functionality. 原因是我需要访问beforeSend功能。

The load that would work for me is 对我有用的负担是

$('tableCell').load('this/is/a/url #table');

This would obviously load the element table into tableCell. 显然,这会将元素表加载到tableCell中。

The ajax that I have currently is 我目前拥有的ajax是

$.ajax({
    url: 'this/is/a/url',
    beforeSend: function() {
        //STUFF
    },
    success: function(html) {
        //Not sure what to put here?
    }
});

My question is what do I do in the success function to only load table into table cell. 我的问题是在成功函数中仅将表加载到表单元中该怎么办。

I've tried things like 我已经尝试过类似的事情

$('#tableCell').html(html.getElementById('table'));

but that didn't seem to work, I've also tried some other combinations of js and jquery... but no luck. 但这似乎没有用,我还尝试了js和jquery的其他组合...但是没有运气。

Hopefully someone can help me out! 希望有人可以帮助我!

Thanks. 谢谢。

这应该可以工作: $('#tableCell').html($(html).find('#table'));

You pretty much have it. 你几乎拥有它。 Double check your selectors to make sure they're matching something; 仔细检查您的选择器,以确保它们匹配某些内容; it's unclear reading your examples what the ID of the table actually is. 目前尚不清楚阅读您的示例表的实际ID。

// append to the table (i think this is how load() works)
$(html).find('#table').appendTo('#tableCell');

or 要么

// replace the data
$('#tableCell').html( $(html).find('#table') );

..assuming #tableCell is in the DOM and #dt_report is in the AJAX response. 假设#tableCell位于DOM中,而#dt_report位于AJAX响应中。

Also, if #table is the root node in the variable "html", you will have to use filter() instead of find(), but I don't think that's the case here. 另外,如果#table是变量“ html”中的根节点,则必须使用filter()而不是find(),但我认为情况并非如此。

One option would be to actually put the html content you receive from the AJAX call somewhere on your page and hide it and then use the normal jQUery selectors to pick out what you need from the hidden table. 一种选择是将您从AJAX调用中接收到的html内容实际放置在页面上的某个地方,然后将其隐藏,然后使用常规的jQUery选择器从隐藏表中选择所需内容。

The best case would honestly be to only get what you need in the AJAX call but I know this isn't necessarily always possible. 老实说,最好的情况是仅在AJAX调用中获得所需的内容,但是我知道这不一定总是可能的。

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

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