简体   繁体   English

在第二个ajax调用后,所有jquery代码均不起作用

[英]after second ajax call all jquery codes not working

I'm getting a partial via ajax in asp.net mvc, it work fine for first time and second time but after that, all jquery codes stop working. 我通过asp.net mvc中的ajax获得了部分,第一次和第二次都可以正常工作,但是之后,所有的jQuery代码都停止工作。 this is my code : 这是我的代码:

<script>

    var jqgd = jQuery.noConflict();
    jqgd(function () {
        jqgd('#getdata-@ViewBag.term').on('click', '#getdata-@ViewBag.term a', function () {
            if (this.href == "") { return; }
            jqgd.ajax({
                url: this.href,
                type: 'GET',
                cache: false,
                success: function (result) {
                    alert(result);
                    jqgd('#retrieve-@ViewBag.term').html(result);
                }
            });
            return false;
        });
    });
</script>

<script type='text/javascript'> //This function
    jQuery(function ($) {

        $("div, p, a, b, strong, bold, font, span, td")
        .filter(function () {
            return $(this).children(":not(.word)").length == 0
        })
        .each(function () {
            this.innerHTML = $(this).text().replace(/\S+/g, function (word) {
                return "<span class='word'>" + word + "</span>";
            });

            $(".word", this).filter(isEnglish).addClass('english');
            $(".word", this).filter(isPersian).addClass('persian');
        });

        function isEnglish() {
            return $(this).text().charCodeAt(0) < 255;
        }

        function isPersian() {
            return $(this).text().charCodeAt(0) > 255;
        }
    });
</script>

<div id="retrieve-@ViewBag.term">
    <div style="float:right;">
        <div class="searchtitles" style="float: right;">@ViewBag.term</div>
        <table class="jjt" cellpadding="0" cellspacing="0">
            <tr class="j2t">
                <td class="j13t">field</td>
                <td class="j13t">field</td>
                <td class="j13t">field</td>
                <td class="j13t">field</td>
                <td class="j13t">field</td>
                <td class="j13t">field</td>
                <td class="j13t">field</td>
            </tr>
            @foreach (var item in ViewBag.Terms)
            {
                Mydata 
            }


        </table>

        <div id="getdata-@ViewBag.term" style="float:left; direction:ltr; margin-left:-20px; margin-top:-15px;">@Html.PagedListPager((IPagedList)ViewBag.Tours, page => Url.Action("results", new { page }))</div>
    </div>
</div>

part of code that i comment as ' This function ' in code does not work. 我在代码中注释为“ 此函数 ”的部分代码不起作用。 also I use tipsy popup in my layout, it does not work after second call. 我在布局中也使用了提示式弹出窗口,在第二次调用后无法使用。 it seems all of my jquery codes stop working after second call. 似乎我的所有jquery代码在第二次调用后都停止工作。

what is problem? 有什么问题

try something like this 尝试这样的事情

    <script>
        var jqgd = jQuery.noConflict();
        jqgd(function () {
            jqgd('#getdata-@ViewBag.term').on('click', '#getdata-@ViewBag.term a', function () {
                if (this.href == "") { return; }
                jqgd.ajax({
                    url: this.href,
                    type: 'GET',
                    cache: false,
                    success: function (result) {
                        alert(result);
                        jqgd('#retrieve-@ViewBag.term').html(result);
                        init();
                    }
                });
                return false;
            });
        });
    </script>

    <script type='text/javascript'> //This function
        function isEnglish() {
            return $(this).text().charCodeAt(0) < 255;
        }

        function isPersian() {
            return $(this).text().charCodeAt(0) > 255;
        }
        function init(){
         $("div, p, a, b, strong, bold, font, span, td")
            .filter(function () {
                return $(this).children(":not(.word)").length == 0
            })
            .each(function () {
                this.innerHTML = $(this).text().replace(/\S+/g, function (word) {
                    return "<span class='word'>" + word + "</span>";
                });

                $(".word", this).filter(isEnglish).addClass('english');
                $(".word", this).filter(isPersian).addClass('persian');
            });

        }
        jQuery(function ($) {
            init();
        });
    </script>

Simply make function of your code that you want to execute. 只需使您要执行的代码的功能。

Because your code run on initial element which are present in DOM on DOM ready. 因为您的代码运行在DOM就绪的DOM中存在的初始元素上。

After ajax you get new element on which code doesn't run so put your code in function and call it on DOM ready and after your ajax call. 在ajax之后,您将获得一个新元素,该元素不会在其上运行代码,因此将您的代码放入函数中,并在DOM准备就绪以及ajax调用之后调用它。

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

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