2012-10-22 2 views
0

J'utilise la méthode $ jquery ajax pour trier les données et afficher dans 3 champs, j'ai utilisé la même méthode ajax pour 3 textboxs différents, To, CC, Bcc, et je veux utiliser la méthode 1 ajax pour les données, ici tagSource: one fonction à appeler la méthode ajaxComment peut-on réutiliser la méthode jQuery ajax pour 3 fonctions?

jQuery(document).ready(function() { 
      jQuery("#totags").tagit({ 
       allowSpaces: true, 
       tagSource: function (request, response) { 
        $.ajax({ 
         type: "POST", 
         url: "Blog.aspx/GetName", 
         data: "{'match': '" + request.term + "'}", 
         dataType: "json", 
         contentType: "application/json", 
         success: function (data) { 
          console.log(data); 
          if (data.d != null) { 
           response($.map(data.d, function (item) { 
            return { 
             label: item.Name, 
             value: item.id, 
            }; 
           })); 
          } 
         } 
        }); 
       }, 
      }); 
      jQuery("#cctags").tagit({ 
       allowSpaces: true, 
       tagSource: function (request, response) { 
        $.ajax({ 
         type: "POST", 
         url: "Blog.aspx/GetName", 
         data: "{'match': '" + request.term + "'}", 
         dataType: "json", 
         contentType: "application/json", 
         success: function (data) { 
          console.log(data); 
          if (data.d != null) { 
           response($.map(data.d, function (item) { 
            return { 
             label: item.Name, 
             value: item.id, 
            }; 
           })); 
          } 
         } 
        }); 
       }, 
      }); 
      jQuery("#bcctags").tagit({ 
       allowSpaces: true, 
       tagSource: function (request, response) { 
        $.ajax({ 
         type: "POST", 
         url: "Blog.aspx/GetName", 
         data: "{'match': '" + request.term + "'}", 
         dataType: "json", 
         contentType: "application/json", 
         success: function (data) { 
          console.log(data); 
          if (data.d != null) { 
           response($.map(data.d, function (item) { 
            return { 
             label: item.Name, 
             value: item.id, 
            }; 
           })); 
          } 
         } 
        }); 
       }, 
      }); 
     }); 

Répondre

3

Vous pouvez sélecteurs de chaîne en utilisant jQuery:

jQuery(document).ready(function() { 
    jQuery("#totags, #cctags, #bcctags").tagit({ 
     // .. 
    }); 
}); 
0

en cas général assigner à var:

var ajaxcall=function(request, response) { 
    $.ajax({ 
        type: "POST", 
        url: "Blog.aspx/GetName", 
        data: "{'match': '" + request.term + "'}", 
        dataType: "json", 
        contentType: "application/json", 
        success: function(data) { 
            console.log(data); 
            if (data.d != null) { 
                response($.map(data.d, function(item) { 
                    return { 
                        label: item.Name, 
                        value: item.id, 
                    }; 
                })); 
            } 
        } 
    }); 
};​ 

     jQuery("#totags").tagit({ 
      allowSpaces: true, 
      tagSource: ajaxcall 
     }); 
     jQuery("#cctags").tagit({ 
      allowSpaces: true, 
      tagSource: ajaxcall 
     }); 
     jQuery("#bcctags").tagit({ 
      allowSpaces: true, 
      tagSource: ajaxcall 
     }); 

i Dans un cas particulier, utilisez la chaîne comme Florent a répondu.

Questions connexes