tags:

views:

397

answers:

3

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 'currentTab' 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>
+4  A: 

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(); 
}
Chris Pebble
Returning false within an event handler on an <a> element does prevent navigation, just the same as preventDefault(). I agree that preventDefault() is preferable, but I don't think its his problem.
Dave Ward
I had the same though initially but `return false` wouldn't work in this case as it would also fire `stopPropogation()` which would prevent the event from bubbling up to the `li`.
Chris Pebble
+1  A: 

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.

PanJanek
A: 

So i have tried splitting this out into two click functions. One on the li and one on the a:

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

    // Find the current tab
    var $oldTab = $('ul#coverTabs > li').index ($('.currentTab'));
    var $oldTab = $oldTab + 1

    // Removes default class applied in HTML and onClick adds 'currentTab' class
    $(".currentTab").removeClass("currentTab");
    $(this).addClass("currentTab"); 

    // Find the current tab
    var $newTab = $('ul#coverTabs > li').index ($('.currentTab'));
    var $newTab = $newTab + 1

    // If tab has changed trigger highlighting of default column
    if ($newTab != $oldTab) {
        $defaultCell.trigger('click');
    };

    // Find number of li
    var $tabIndex = $(this).parent().children().index ($(this));
    var $tabIndex = $tabIndex + 1;       

    // Find the current column
    var $currentLevel = $('table#benefit > thead > tr > th').index ($('.highLight'));

    // Find the current tab
    var $currentTab = $('ul#coverTabs > li').index ($('.currentTab'));
    var $currentTab = $currentTab + 1       

    // Retrieve cost data on tab change

    return false;       

});

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

    // 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');           

    return false;   

});

Now the .load does not work at all. Thanks for your help thus far.

RyanP13
Why are you using this as URL: $tabValue + ' #' + 'benefit' ? I don't think that using #fragment in ajax URL is allowed. Use $('#benefit').load($tabValue) instead.
PanJanek
$('#benefit').load($tabValue + ' #' + 'benefit');This looks for the table with id 'benefit' and then loads that HTML into the same position.I thought this was allowed with the high level .load method.
RyanP13
If i use: $('ul#coverTabs > li > a').click(function(event) { // Find href of current tab var $tabValue = $('ul#coverTabs > li.currentTab > a').attr('href'); // Load new table based on href URL variables $('.benefitWrap').load($tabValue + ' .' + 'benefitWrap'); event.preventDefault(); });Instead of return false then this works in Firefox but not IE :(
RyanP13
Try to call event.preventDefault() as a first statment inside the handler function, and return false anyway at the end. Althought the answer for this question: http://stackoverflow.com/questions/1357118/javascript-event-preventdefault-vs-return-false says that returning false should be enough, maybe there there are some compatibility issues.
PanJanek