简体   繁体   English

在第二个ajax调用jquery后无法正常工作

[英]after second ajax call jquery 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, 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? 我正在使用pagedlist,当我第二次更改页面后,似乎所有的jQuery命令都终止了,这是什么问题? can anyone help me ? 谁能帮我 ?

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>

Your #getdata-@ViewBag.term is within the #retrieve-@ViewBag.term element but that gets replaced after the first call. 您的#getdata-@ViewBag.term#retrieve-@ViewBag.term元素内,但在第一次调用后将被替换。 You need to use event delegation. 您需要使用事件委托。

Try this instead: 尝试以下方法:

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

This is just a shot in the dark: 这只是黑暗中的一枪:

Get e from your click handler then try e.preventDefault(); 从点击处理程序中获取e,然后尝试e.preventDefault(); before the return false. 返回前为假。

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

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