For avoid repeating i want a function use in several ajax call. how is it?
Like:
//use of content this function in other function(ajax call)
$(function num_bg () {
var total = $('.pag a', '.ser').size();
if (total == 0) {
$('.number').css('display','none');
}
});
// First ajax call
function pagination(e) {
e.preventDefault();
var dataObj = $('form').serialize();
$.ajax({
type: "POST",
dataType: "html",
url: 'dirc',
data: dataObj,
cache: false,
success: function (html) {
var $html = $(html);
$('#num_count').replaceWith($html.find('#num_count'));
$('tr#paginate').replaceWith($html.find('tr#paginate'));
开发者_如何学Python $('.pagination').replaceWith($html.find('.pagination'))
$('#erro_find').remove();
num_bg (); // This is same function above (this don't work)
}
});
return false;
}
$('form').live('change', pagination);
$('.pag a').live('click', pagination);
$('#input').live('keyup', pagination);
//Second ajax call
$('#delete').click(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: 'diuarsl',
data: dataString,
cache: false,
success: function(html){
var $html = $(html);
$('#num').replaceWith($html.find('#num_count'));
$('tr#pag').replaceWith($html.find('tr#paginate'));
$('.pag').replaceWith($html.find('.pagination'));
num_bg (); // This is same function above (this don't work)
}
}
})
});
The issue is that you have your function wrapped up in a closure. Closures are useful this way, since it means you're not polluting any other namespace with your variables (e.g. the window).
Remove the $(...)
around your newbg function.
// num_bg is now available as a global
function num_bg () {
var total = $('.pag a', '.ser').size();
if (total == 0) {
$('.number').css('display','none');
}
}
$( num_bg ); // invoke the function when the DOM is ready
The $(function() { });
in jQuery will attach a handler to the DOM ready event. Usually if you're using the DOM then you'd want to wrap all the code in it.
Just remove the $(..)
call around the Function Expression (and make it a Function Declaration).
精彩评论