开发者

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜