jquery ajax问题

发布于 2024-09-24 17:47:53 字数 1220 浏览 2 评论 0原文

感谢你们迄今为止给我的每一个帮助! :D

现在,我在使用 jquery 和 ajax 时遇到了一些问题

应用 jquery 分页插件“pajinate”

$(function(){
    $('#talent_paging').pajinate({
        num_page_links_to_display : 4,
        items_per_page : 15,
        nav_label_first : '«',
        nav_label_prev : '<',
        nav_label_next : '>',
        nav_label_last : '&raquo;'
    });
});

,我从数据库中获取所有用户的照片,将它们调用到网格中,然后使用以下代码源 : jquery.pajinate

该页面是关于使用某些参数,例如:年龄、性别和关键字的范围 我正在使用 ajaxform 来处理表单提交,使用此代码

$(function() { 
    var options = { 
        target:'#talent_paging',
    };
    $('#search_talent').ajaxForm(options); 
});

源: ajaxForm

就像你一样我猜,分页在第一页加载时运行良好,但是一旦我进行搜索,它就失败了。当我使用jquery数据表并尝试操作每一行时,我实际上遇到了类似的问题,它在第一页上运行良好,但在下一页上失败了。

我使用 .delegate() 解决了我的数据表的问题,我认为这个问题也有同样的问题,我尝试了几种方法在我的分页搜索问题上添加委托方法,但这只是试验,不知道我实际上在做什么(复制此粘贴:p)因为我不太明白 .delegate() 是如何工作的

,所以请帮助我解决这些问题

是委托是解决我当前问题的最佳方法?

如果是,请帮助我理解 .delegate() 是如何工作的,

谢谢

thanks for every help that you guys have given me so far !! :D

and now, i encounter some problem on me using jquery and ajax

i am fetching all of my user's photos from my database, calling them out into a grid and then applying a jquery pagination plug-in 'pajinate' using this code

$(function(){
    $('#talent_paging').pajinate({
        num_page_links_to_display : 4,
        items_per_page : 15,
        nav_label_first : '«',
        nav_label_prev : '<',
        nav_label_next : '>',
        nav_label_last : '»'
    });
});

source: jquery.pajinate

the page is about searching using certain parameter such as: range of age, gender, and keywords
and i am using ajaxform to process the form submission, using this code

$(function() { 
    var options = { 
        target:'#talent_paging',
    };
    $('#search_talent').ajaxForm(options); 
});

source: ajaxForm

like you would have guessed, the pagination is working well on the first page load, but as soon as i operate the search, it fails me. I actually had encountered similar problem regarding this when i was using jquery datatable and was trying to manipulate each row, it was working well on the first page, but it fails on the next page.

i solved my datatable's problem using .delegate(), and i figure that this one has the same issue, i have tried several ways on adding the delegate methods on my pagination-search problem but it was only trials without knowing what am i actually doing (copy this paste that :p) since i don't really understand how does .delegate() works

so, please help me on these questions

is delegate the best way to solve my current problem ??

if it is, please help me on understanding how does .delegate() works

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

倾城泪 2024-10-01 17:47:53

$().delegate() 仅适用于事件,例如单击、鼠标悬停、焦点等。Javascript 中有一个概念称为“冒泡”。这意味着,除非您明确另有说明,否则每个事件都会在 DOM 树中“冒泡”,并在树中的每个元素上触发。这意味着您可以使用共同的祖先元素来“捕获”子元素上特定类型的所有事件。

据我了解,这在 pajnate 插件上不起作用,因为它不使用事件。我相信它在调用时修改了文档。

每次ajaxform完成工作时,您都需要使用回调函数来调用$('#talent_paging').pajinate()

$(function() { 
    var options = {
        target:'#talent_paging',
        success: function() {
            $('#talent_paging').pajinate({
                num_page_links_to_display : 4,
                items_per_page : 15,
                nav_label_first : '«',
                nav_label_prev : '<',
                nav_label_next : '>',
                nav_label_last : '»'
            });
        }
    }

    $('#search_talent').ajaxForm(options); 
});

请注意,这段代码在优化方面并不出色,但它是如果没有看到你的基本 HTML,很难做到这一点。

$().delegate() only works on events, e.g. clicks, mouseovers, focuses, etc. There is a concept in Javascript called "bubbling". This means that, unless you explicitly say otherwise, every event will "bubble" its way up the DOM tree and will be triggered on every element in the tree. This means that you can use a common ancestor element to "trap" all events of a particular type on child elements.

This will not work, as I understand it, on the pajinate plugin, because it does not use events. I believe it modifies the document at the moment of the call.

You need to use a callback function to call $('#talent_paging').pajinate() each time the ajaxform has finished its work:

$(function() { 
    var options = {
        target:'#talent_paging',
        success: function() {
            $('#talent_paging').pajinate({
                num_page_links_to_display : 4,
                items_per_page : 15,
                nav_label_first : '«',
                nav_label_prev : '<',
                nav_label_next : '>',
                nav_label_last : '»'
            });
        }
    }

    $('#search_talent').ajaxForm(options); 
});

Note that this code is not excellent in terms of optimisation, but it's hard to do that without seeing your base HTML.

↘紸啶 2024-10-01 17:47:53

您使用的是 IE8 之前的 IE 吗?如果是这样,这可能就是问题所在——你曾经遇到过语法错误(或者至少是歧义),但据我所知,只有 IE 会关心它:

$(function() { 
    var options = { 
        target:'#talent_paging', // <=== This comma is the error
    };
    $('#search_talent').ajaxForm(options); 
});

对象文字的语法直到最近才明确允许尾随逗号。 SpiderMonkey (Firefox)、V8 (Chrome) 以及 Safari 和 Opera 使用的任何内容都不在乎,但在 JScript 6 (IE8) 之前,JScript (IE) 会在逗号上引发解析异常,并且您的脚本会终止。如果您使用数组文字执行此操作,则会发生类似但不同的情况:

var a = [1, 2, 3, 4, 5, ];

IE (JScript) 创建一个包含六个(是的,六个)条目的数组,其中最后一个是未定义。这并非不合理,因为我们总是允许有空白条目(例如,var a = [1, , 3];),并且这些条目默认为undefined,但其他人却走了相反的路,创建了一个包含五个条目的数组。

最近的第五版规范澄清了这一点(耶!) 。在对象字面量(第 11.1.5 节)和数组字面量(第 11.1.4 节)中明确允许使用尾随逗号,并且在数组字面量的情况下,它不会添加到数组的长度(上面的a.length是5)。

IE 的最新发布版本使用 JScript 6,它现在允许在对象文字上使用尾随逗号,但仍然存在数组末尾额外条目的问题。希望现在 ECMAScript 5 已经解决了这一问题,微软 JScript 的下一个版本将改变这一点,尽管这对他们来说必须更难推销......

Are you using IE prior to IE8? If so, that could be the problem — you have what used to be a syntax error (or at least ambiguity), but AFAIK only IE will care about it:

$(function() { 
    var options = { 
        target:'#talent_paging', // <=== This comma is the error
    };
    $('#search_talent').ajaxForm(options); 
});

The grammar for object literals didn't until recently explicitly allow a trailing comma. SpiderMonkey (Firefox), V8 (Chrome), and whatever Safari and Opera use don't care, but prior to JScript 6 (IE8), JScript (IE) throws a parsing exception on the comma and your script dies. A similar but different thing happens if you do it with an array literal:

var a = [1, 2, 3, 4, 5, ];

IE (JScript) creates an array with six (yes, six) entries, the last of which is undefined. This isn't unreasonable, because we were always allowed to have blank entries (e.g., var a = [1, , 3];) and those entries defaulted to undefined, but everyone else went the other way and created an array with five entries.

The recent 5th edition specification clears this up (yay!). The trailing comma is explicitly allowed in object literals (Section 11.1.5) and array literals (Section 11.1.4), and in the case of array literals it doesn't add to the length of the array (a.length above is 5).

The latest released version of IE uses JScript 6, which now allows the trailing comma on object literals, but still has the issue with the extra entry at the end of the array. Hopefully now that ECMAScript 5 nailed that down, the next version of Microsoft's JScript will change that, although it has to be a harder sell for them...

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