2011-07-07 66 views
0
$(function() { 
    $("#rv-title").click(function(event) { 
    $("#rv-content").toggle(); 



    $.ajax({ 
     url: 'index.php?route=check', 
     dataType: 'json', 
     success: function(json) { 
      if (json['output']) { 
       $('#cart .content').html(json['output']); 
      } 
     } 
    }); 


    event.stopPropagation(); 
}); 


$(document).click(function(event) { 
    var a = $(event.target).andSelf().parents("#rv-content"); 
    if (a.length == 0 && $("#rv-content").is(":visible")) { 
     $("#rv-content").toggle(); 
     } 
    }); 
}); 

我的ajax函數在哪裏,是放它的地方嗎?jquery ajax裏面的函數

感謝

回答

2

試試這個

$("#rv-content").toggle(function(){ 
    //your ajax call 

    }); 

$("#rv-content").toggle(function(){ 

my_ajax(); 

}); 

function my_ajax(){ 

//ajax call 

} 
+0

在您看來,哪一個更有效? –

+0

第二個,因爲你可以多次調用my_ajax()函數。因爲。再次寫同樣的想法是浪費時間。 – Gowri

1

你也可以傳遞函數作爲參數傳遞給您的通話切換。 例如

$("#rv-content").toggle(function(){ /* put your code here... */ }); 

見: http://api.jquery.com/toggle/