开发者

how to refactor this jQuery(only AJAX URL different)

开发者 https://www.devze.com 2023-01-11 10:09 出处:网络
function one(R) { # do some thing.. $.ajax({ \'url\': \'/one?search=\' + R.val(), #some thing common here
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

精彩评论

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