2014-03-05 253 views
2

我在asp.net mvc中通過ajax獲得了一個部分,它在第一次和第二次都能正常工作,但之後它重定向到通過ajax獲取頁面的頁面instad。第二次ajax調用jquery不工作

這是我的部分頁面代碼:

<script> 

    var jqgd = jQuery.noConflict(); 
    jqgd(function() { 
     jqgd('#[email protected]').on('click', 'a', function() { 
      if (this.href == "") { return; } 
      jqgd.ajax({ 
       url: this.href, 
       type: 'GET', 
       cache: false, 
       success: function (result) { 
        alert(result); 
        jqgd('#[email protected]').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="[email protected]"> 
    <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="[email protected]" 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> 

我使用pagedlist,當我第二次修改後的頁面,似乎所有的jQuery讚揚終止,是什麼問題? 任何人都可以幫助我嗎?

編輯:這部分代碼在第二次調用後不起作用。

<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> 
+0

沒有嘗試'活(「點擊」)',而不是'on' – madhushankarox

+0

@madhushankarox:它不工作... –

回答

1

#[email protected]#[email protected]元素中但被第一個電話後更換。您需要使用事件委派。

試試這個:

jqgd('#[email protected]').on('click', '#[email protected] a', function() { 
    ... 
} 
+0

哇,它的正常工作。謝謝賈森 –

+0

我的jQuery代碼中的一部分我頁面,也不起作用。我把它複製到我的問題,請告訴我什麼是錯的? –

+0

我爲你編輯我的問題。預先感謝你 –

0

這僅僅是一個在黑暗中拍攝:

從你的點擊處理程序獲取Ë然後嘗試e.preventDefault();之前返回false。