jquery在ajax调用后绑定函数和触发器

发布于 2024-12-09 16:38:39 字数 802 浏览 1 评论 0原文

function bindALLFunctions() {
  ..all triggers functions related go here
};


$.ajax({
        type: 'POST',
        url: myURL,
        data: { thisParamIdNo: thisIdNo },
        success:    function(data){
                        $(".incContainer").html(data);
                        bindALLFunctions();
        },
        dataType: 'html'
});

我是 ajax 和 JQuery 新手。 我的 js-jquery 代码中有上述 ajax 调用。 bindALLFunctions(); 用于在ajax调用后重新调用所有触发器和函数。它工作得很好,正如预期的那样。但是,我在某处读到最好在初始操作完成后加载某些内容,因此我尝试添加/编辑以下两个内容,但没有成功。 有什么想法吗?

1) ->    $(".incContainer").html(data, function(){
                                          bindALLFunctions(); 
                                        });

2) ->    $(".incContainer").html(data).bindALLFunctions();
function bindALLFunctions() {
  ..all triggers functions related go here
};


$.ajax({
        type: 'POST',
        url: myURL,
        data: { thisParamIdNo: thisIdNo },
        success:    function(data){
                        $(".incContainer").html(data);
                        bindALLFunctions();
        },
        dataType: 'html'
});

I am new to ajax and JQuery.
I have the above ajax call in my js-jquery code. bindALLFunctions(); is used to re-call all the triggers and functions after the ajax call. It works all fine and good as expected. However, I have read somewhere that is better to load something after the initial action is finished, so I have tried to add/edit the following two without any success.
Any ideas?

1) ->    $(".incContainer").html(data, function(){
                                          bindALLFunctions(); 
                                        });

2) ->    $(".incContainer").html(data).bindALLFunctions();

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

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

发布评论

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

评论(3

药祭#氼 2024-12-16 16:38:39

也许您应该看看 live委托 函数。您可以在应用程序开始时设置一个唯一的事件处理程序,所有加载的 ajax 代码都将自动绑定:

$("table").delegate("td", "hover", function(){
    $(this).toggleClass("hover");
});

但如果您更喜欢使用 Jquery.ajax 调用,则必须执行以下操作:

$.ajax({
        type: 'POST',
        url: myURL,
        data: { thisParamIdNo: thisIdNo },
        success:    function(data){
                        $(".incContainer").html(data);
                        bindALLFunctions(".incContainer");
        },
        dataType: 'html'
});

并转换 bindALLFunctions as:

function bindALLFunctions(selector) {
  ..all triggers functions related go here. Example:
  $('#foo', selector).bind('click', function() {
     alert('User clicked on "foo."');
  });
};

只会将事件绑定在给定选择器“下”。

Perhaps you should have a look to the live and delegate functions. You can set a unique event handler at the beggining of your app and all your loaded ajax code will be automatically binded:

$("table").delegate("td", "hover", function(){
    $(this).toggleClass("hover");
});

But if you prefer to use Jquery.ajax call you have to do something like this:

$.ajax({
        type: 'POST',
        url: myURL,
        data: { thisParamIdNo: thisIdNo },
        success:    function(data){
                        $(".incContainer").html(data);
                        bindALLFunctions(".incContainer");
        },
        dataType: 'html'
});

and transform bindALLFunctions as:

function bindALLFunctions(selector) {
  ..all triggers functions related go here. Example:
  $('#foo', selector).bind('click', function() {
     alert('User clicked on "foo."');
  });
};

that will only bind events "under" the given selector.

落在眉间の轻吻 2024-12-16 16:38:39

你最初的代码很好。新版本不起作用,因为 html() 函数没有回调函数。

Your initial code was fine. The new version does not work because html() function does not have a callback function.

烟柳画桥 2024-12-16 16:38:39

很难从你的问题中看出你想问什么,但我猜你想知道 ready 函数。它可以让您在文档可用后调用bindALLFunctions;只需执行 $(document).ready(bindALLFunctions)$(document).ready(function() { bindALLFunctions(); }) 即可。

It's hard to tell from your question just what you intend to ask, but my guess is that you want to know about the ready function. It would let you call your bindALLFunctions after the document was available; just do $(document).ready(bindALLFunctions) or $(document).ready(function() { bindALLFunctions(); }).

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