第二AJAX调用jQuery后不工作工作、AJAX、jQuery

2023-09-10 19:48:23 作者:吃可爱长大

我通过AJAX在asp.net mvc的获取部分,它做工精细的第一次和第二次,但在那之后,它重定向到通过AJAX获得页面的页面instad。

这是我的部分页面code:

 <脚本>

    变种jqgd = jQuery.noConflict();
    jqgd(函数(){
        jqgd('#getdata-@ViewBag.term')。在('点击','A',函数(){
            如果(this.href ==){返回; }
            jqgd.ajax({
                网址:this.href,
                键入:GET,
                缓存:假的,
                成功:函数(结果){
                    警报(结果);
                    jqgd('#retrieve-@ViewBag.term)HTML(结果)。
                }
            });
            返回false;
        });
    });
< / SCRIPT>

<脚本类型=文/ JavaScript的'> // 添加
    jQuery的(功能($){

        $(DIV,P,A,B,坚强,勇敢,字体,跨度,TD)
        .filter(函数(){
            返回$(本)。儿童(:不是(.word))的长度== 0。
        })
        每个(函数(){
            this.innerHTML = $(本)的.text()代替(/ \ s + /克,功能(字){
                返回<跨度类='字'>中+文字+< / SPAN>中;
            });

            $(字,这一点).filter(isEnglish).addClass('英语');
            $(字,这一点).filter(isPersian).addClass('波斯');
        });

        传播isEnglish(){
            。返回$(这)的.text()字符$ C $猫(0)< 255;
        }

        功能isPersian(){
            。返回$(这)的.text()字符$ C $猫(0)> 255;
        }
    });
< / SCRIPT>

< D​​IV ID =retrieve-@ViewBag.term>
    < D​​IV的风格=浮动:权利;>
        < D​​IV CLASS =searchtitles的风格=浮动:权利;> @ ViewBag.term< / DIV>
        <表类=JJT的cellpadding =0CELLSPACING =0>
            &所述; TR类=j2t>
                < TD类=j13t>场< / TD>
                < TD类=j13t>场< / TD>
                < TD类=j13t>场< / TD>
                < TD类=j13t>场< / TD>
                < TD类=j13t>场< / TD>
                < TD类=j13t>场< / TD>
                < TD类=j13t>场< / TD>
            < / TR>
            @foreach(在ViewBag.Terms VAR项)
            {
                MYDATA
            }


        < /表>

        < D​​IV ID =getdata-@ViewBag.term的风格=浮动:左;方向:LTR;保证金左:-20px;的margin-top:-15px;> @ Html.PagedListPager((IPagedList)ViewBag .Tours,页面=> Url.Action(结果,新{页}))≤; / DIV>
    < / DIV>
< / DIV>
 

我使用pagedlist,当我似乎所有的jQuery赞扬终止后第二次换页,是什么问题呢? 谁能帮助我?

修改:这部分的code没有第二个电话后,工作太

 <脚本类型=文/ JavaScript的'> // 添加
        jQuery的(功能($){

            $(DIV,P,A,B,坚强,勇敢,字体,跨度,TD)
            .filter(函数(){
                返回$(本)。儿童(:不是(.word))的长度== 0。
            })
            每个(函数(){
                this.innerHTML = $(本)的.text()代替(/ \ s + /克,功能(字){
                    返回<跨度类='字'>中+文字+< / SPAN>中;
                });

                $(字,这一点).filter(isEnglish).addClass('英语');
                $(字,这一点).filter(isPersian).addClass('波斯');
            });

            传播isEnglish(){
                。返回$(这)的.text()字符$ C $猫(0)< 255;
            }

            功能isPersian(){
                。返回$(这)的.text()字符$ C $猫(0)> 255;
            }
        });
    < / SCRIPT>
 
ajax 跨域请求数据,JQuery Ajax执行跨域请求数据的解决方案

解决方案

#getdata-@ViewBag.term 是在#retrieve- @ ViewBag.term 元素,但在第一次调用之后被替换。您需要使用事件代表团。

试试这个来代替:

  jqgd('#retrieve-@ViewBag.term')。在(点击,'#getdata-@ViewBag.term一',函数(){
    ...
}
 

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.

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 ?

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. You need to use event delegation.

Try this instead:

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