之后,第二个ajax调用jquery不起作用。

时间:2022-12-01 09:13:24

I'm getting a partial via ajax in asp.net mvc, it work fine for first time and second time but after that, it redirect to page instad of getting page via ajax.

我在asp.net mvc中通过ajax得到了一个部分,它在第一次和第二次中都很好,但是之后,它重定向到通过ajax获取页面的页面instad。

this is my partial page code :

这是我的部分页面代码:

<script>

    var jqgd = jQuery.noConflict();
    jqgd(function () {
        jqgd('#getdata-@ViewBag.term').on('click', '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'> // Added
    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>

i'm using pagedlist and when i change page after second time it seems all jquery commends terminated, what is problem? can anyone help me ?

我使用的是pagedlist,当我第二次修改页面时,似乎所有jquery推荐都终止了,有什么问题吗?谁能帮帮我吗?

EDIT : this part of code doesn't work too after second call.

编辑:这部分代码在第二次调用后也不能工作。

<script type='text/javascript'> // Added
        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>

2 个解决方案

#1


1  

Your #getdata-@ViewBag.term is within the #retrieve-@ViewBag.term element but that gets replaced after the first call. You need to use event delegation.

你的# getdata -@ViewBag。术语在#检索-@ViewBag中。term元素,但是在第一次调用之后会被替换。您需要使用事件委托。

Try this instead:

试试这个:

jqgd('#retrieve-@ViewBag.term').on('click', '#getdata-@ViewBag.term a', function () {
    ...
}

#2


0  

This is just a shot in the dark:

这只是瞎猜而已:

Get e from your click handler then try e.preventDefault(); before the return false.

从单击处理程序获取e,然后尝试preventdefault ();在返回false。

#1


1  

Your #getdata-@ViewBag.term is within the #retrieve-@ViewBag.term element but that gets replaced after the first call. You need to use event delegation.

你的# getdata -@ViewBag。术语在#检索-@ViewBag中。term元素,但是在第一次调用之后会被替换。您需要使用事件委托。

Try this instead:

试试这个:

jqgd('#retrieve-@ViewBag.term').on('click', '#getdata-@ViewBag.term a', function () {
    ...
}

#2


0  

This is just a shot in the dark:

这只是瞎猜而已:

Get e from your click handler then try e.preventDefault(); before the return false.

从单击处理程序获取e,然后尝试preventdefault ();在返回false。