简体   繁体   English

每次使用ajax从WebMethod加载数据时如何运行脚本?

[英]how to make a script run every time i load data from WebMethod using ajax?

i have a problem that javascript script only loaded once when the page is loading,but i need it every time i get data from WebMethod on the server side 我有一个问题,即javascript脚本只在加载页面时加载一次,但每次我从服务器端的WebMethod获取数据时都需要它

here is my code: 这是我的代码:

My JavaSript ajax: 我的JavaSript ajax:

<script type="text/javascript">


    $(document).ready(function () {

        function InfiniteScroll() {

            $('#divPostsLoader').html('<img src="img/loader.gif">');

            $.ajax({
                type: "POST",
                url: "Home.aspx/GetLastArticles",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    if (data != "") {

                        $('#divPostsLoader:last').after(data.d);

                    }
                    $('#divPostsLoader').empty();
                },
                error: function (data) {
                    if (!document.getElementById('#divPostsLoader').innerHTML == "<p>END OF POSTS</p>")
                        $('#divPostsLoader').empty();
                    $('#divPostsLoader').html("<p>END OF POSTS</p>");
                }
            })

        };
        var b = false;
        //When scroll down, the scroller is at the bottom with the function below and fire the lastPostFunc function
        $(window).scroll(function () {

            if ($(document).height() <= $(window).scrollTop() + $(window).height()) {
                InfiniteScroll();

            }

            b = true;
        });
        if (!b) {

            window.onload = function () {
                InfiniteScroll();

            };

        }
    });

</script>

and here is my javasript that i want it to fire everytime i get data from GetLastArticles WebMethod: 这里是我的javasript,我希望每次从GetLastArticles WebMethod获取数据时它都会触发:

<script type="text/javascript">
    $(".tag_button").on('click', function (e) {
        e.preventDefault(); // prevent default action - hash doesn't appear in url
        $(this).parent().find('div').toggleClass('tags-active');
        $(this).parent().toggleClass('child');


    });
</script>

my aspx page (client side): 我的aspx页面(客户端):

<div class="tags_list">
                                    <a class="tag-icon tag_button" href="#" >Tags</a>
                                    <div class="tags">
                                        <a class="tag-icon" href="#" >Tag1</a>
                                        <a class="tag-icon" href="#">Tag2</a>
                                        <a class="tag-icon" href="#">Tag3</a>
                                        <a class="tag-icon" href="#">Tag4</a>
                                        <a class="tag-icon" href="#">Tag5</a>
                                        <a class="tag-icon" href="#">Tag6</a>
                                        <a class="tag-icon" href="#">Tag7</a>
                                    </div>
                                </div>

Change to the following and the eventhandler is attached to dynamically added elements too. 更改为以下内容,并且eventhandler也附加到动态添加的元素。

$(document).on('click', '.tag_button', function() {...});

Syntax explanation 语法解释

Just to clarify the code. 只是为了澄清代码。 The syntax is as follows: 语法如下:

$(document).on(events, cssSelector, handler);

See the full API on jQuery API documentation . 查看有关jQuery API文档的完整API By selecting document we make sure this event handler is loaded to all elements in the document now and in the future. 通过选择document我们确保现在和将来将此事件处理程序加载到文档中的所有元素。

call your function in ajax success 在ajax成功中调用你的函数

success: function (data) {
        if (data != "") {
                $('#divPostsLoader:last').after(data.d);
       }
        $('#divPostsLoader').empty();
        $(".tag_button").click();
},

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

相关问题 如何在每次加载特定页面时运行 JavaScript 脚本。 浏览器:Edge(我正在使用 Tampermonkey) - How do I make a JavaScript script run every time a specific page loads. Browser: Edge (I'm using tampermonkey) 当我单击鼠标将其重定向到处理程序(页面)时,如何使javascript每次都加载新数据(不使用缓存数据) - how to make javascript load new data every time (without using cache data) when I redirect it to a handler(page) on mouse click 每次重新加载页面时都运行脚本 - Make script run every time page is reloaded 在C#中使用Jquery Ajax从WebMethod检索数据 - Retrieving data from WebMethod using Jquery Ajax in c# 每次进入组件时,如何使用 useState 使函数运行? - How can I make a function run every time I enter the component, using the useState? 如何通过AJAX状态码从C#Webmethod获取数据? - How to get data from C# Webmethod via AJAX statuscode? jQuery函数ajax不会从webmethod返回数据 - jquery function ajax is not returning data from webmethod 如何使用javascript每x次运行一个脚本? - how to run a script every x time with javascript? 每次页面加载时如何运行脚本 - How to run script every time page loads 我如何使用jquery ajax调用将文件以及其他字段的数据发送到asp.net的web方法? - How can I send files along with other field's data to webmethod is asp.net using jquery ajax call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM