how to refactor this jQuery(only AJAX URL different)
function one(R) {
# do some thing..
$.ajax({
'url': '/one?search=' + R.val(),
#some thing common here
}
function two(R) {
# do some thing..
$.ajax({
'url': '/two?search=' + R.val(),
#some thing common here
}
well, since I'm just doing jQuery, but I guess t开发者_开发技巧his can get improve, refactor?
function search(value, url) {
$.ajax({url: '/'+url+'?search='+val});
};
search(elem.val(), 'one');
or
$.fn.search = function(url) {
return this.each(function() {
$.ajax({url:'/'+url+'?search='+$(this).val()});
});
}
$('input').search('one');
sure you can,
function one(R, url) {
# do some thing..
$.ajax({
'url': url + R.val(),
#some thing common here
}
you might tell us why, what you want to do, and give us some morge code (where does the R come from etc). We might be able to give you more information
精彩评论