开发者

jQuery .load Method causing page refresh AJAX

开发者 https://www.devze.com 2022-12-21 12:37 出处:网络
I have a functional JSP page that accepts URL parameters and updates a table on the page with info based on those parameters.

I have a functional JSP page that accepts URL parameters and updates a table on the page with info based on those parameters.

I have a set of different tabs which pass said URL paraneters to the page they are on so it then re-loads and displays this new data.

I have been trying to use jQuery .load and .ajax methods so that i can pass these URL parameters to the page on the server and then only serve up the table via AJAX rather than doing a whole page refresh.

The problem i am having is that sometimes the page does refresh and i cannot work out why this is happening.

Here is the jQuery:

$('ul#coverTabs > li').live('click', function() {       

    // Removes default class applied in HTML and onClick adds 'curren开发者_C百科tTab' class
    $(".currentTab").removeClass("currentTab");
    $(this).addClass("currentTab"); 

    // Find href of current tab
    var $tabValue = $('ul#coverTabs > li.currentTab > a').attr('href');

    // Load new table based on href URL variables   
    $('#benefit').load($tabValue + ' #' + 'benefit');

    /*$.ajax({ 
      cache: false,
      dataType: "html",
      url: $tabValue, 
      success: function(data) { 
         //var $tableWrap = $('#benefit'); 

         //$('.benefitWrap').append($('.benefitWrap'));

         //alert($tableWrap);
      },
    });*/


    return false;       

});

Here is the HTML for the tabs:

<ul id="coverTabs">
    <li class="currentTab"><a href="quoteAction.do?getBenefitLevelDetails=getBenefitLevelDetails&amp;productPolicyId=748#a1">Individual</a></li>
    <li><a href="quoteAction.do?getBenefitLevelDetails=getBenefitLevelDetails&amp;productPolicyId=749#a1">Couple</a></li>
    <li><a href="quoteAction.do?getBenefitLevelDetails=getBenefitLevelDetails&amp;productPolicyId=750#a1">Family</a></li>
    <li><a href="quoteAction.do?getBenefitLevelDetails=getBenefitLevelDetails&amp;productPolicyId=751#a1">Single Parent Family</a></li>
</ul>


You are returning false when the user clicks on the li element, which doesn't do anything to prevent the a element within the li element from firing. You need to override their default action to prevent them from firing:

$('ul#coverTabs > li > a').click(function(event) {   
    event.preventDefault(); 
}


You should prevent the default action (navigation to link url) in click handler:

<script>
$("a").click(function(event) {
  event.preventDefault();
  // your actions
});
</script>

In your code the default action is not prevented, so when the user clicks the link, browser starts navigating to the HREF URL.

0

精彩评论

暂无评论...
验证码 换一张
取 消