jquery中如何对规则进行分组

发布于 2024-09-03 05:04:01 字数 1536 浏览 5 评论 0 原文

嗨,我有一个简单的问题。我在下面有这段代码,我以非常相似的方式使用了ajax 3次,唯一改变的是传递的数据和目标的id。有没有一种方法可以将这些指令集中到一个简单的指令中? 谢谢 D .

$('#fld_email').focusout(function() {
                    var request_email = $(this).val();              
                    $.ajax({type:"GET",
                            url: "autocomplete.asp",
                            data: "fld=firstname&email="+request_email,
                            beforeSend: function(){$('#fld_firstname').addClass('ac_loading');},
                            success: function(msg){$('#fld_firstname').val(msg);$('#fld_firstname').removeClass('ac_loading'); }
                          });   
                    $.ajax({type:"GET",
                            url: "autocomplete.asp",
                            data: "fld=lastname&email="+request_email,
                            beforeSend: function(){$('#fld_lastname').addClass('ac_loading');},
                            success: function(msg){$('#fld_lastname').val(msg);$('#fld_lastname').removeClass('ac_loading');}
                          });   
                    $.ajax({type:"GET",
                           url: "autocomplete.asp",
                           data: "fld=phone&email="+request_email,
                           beforeSend: function(){$('#fld_phone').addClass('ac_loading');},
                           success: function(msg){$('#fld_phone').val(msg);$('#fld_phone').removeClass('ac_loading');}
                          });   
                    }
                    );

Hi I've got a simple question. I've this code below, i use ajax three times in very similiar ways the only things that change are the data passed and the id of the target. Is there a way to group these instructions in a simple one?
Thx
D.

$('#fld_email').focusout(function() {
                    var request_email = $(this).val();              
                    $.ajax({type:"GET",
                            url: "autocomplete.asp",
                            data: "fld=firstname&email="+request_email,
                            beforeSend: function(){$('#fld_firstname').addClass('ac_loading');},
                            success: function(msg){$('#fld_firstname').val(msg);$('#fld_firstname').removeClass('ac_loading'); }
                          });   
                    $.ajax({type:"GET",
                            url: "autocomplete.asp",
                            data: "fld=lastname&email="+request_email,
                            beforeSend: function(){$('#fld_lastname').addClass('ac_loading');},
                            success: function(msg){$('#fld_lastname').val(msg);$('#fld_lastname').removeClass('ac_loading');}
                          });   
                    $.ajax({type:"GET",
                           url: "autocomplete.asp",
                           data: "fld=phone&email="+request_email,
                           beforeSend: function(){$('#fld_phone').addClass('ac_loading');},
                           success: function(msg){$('#fld_phone').val(msg);$('#fld_phone').removeClass('ac_loading');}
                          });   
                    }
                    );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诺曦 2024-09-10 05:04:01

尝试:

$('#fld_email').focusout(function() {
    var request_email = $(this).val();
    processAjax("fld=firstname&email="+request_email, '#fld_firstname');
    processAjax("fld=lastname&email="+request_email, '#fld_lastname');
    processAjax("fld=phone&email="+request_email, '#fld_phone');
});

function processAjax(data, id){
    $.ajax({type:"GET",
           url: "autocomplete.asp",
           data: data,
           beforeSend: function(){$(id).addClass('ac_loading');},
           success: function(msg){$(id).val(msg).removeClass('ac_loading');}
    });   
}

try:

$('#fld_email').focusout(function() {
    var request_email = $(this).val();
    processAjax("fld=firstname&email="+request_email, '#fld_firstname');
    processAjax("fld=lastname&email="+request_email, '#fld_lastname');
    processAjax("fld=phone&email="+request_email, '#fld_phone');
});

function processAjax(data, id){
    $.ajax({type:"GET",
           url: "autocomplete.asp",
           data: data,
           beforeSend: function(){$(id).addClass('ac_loading');},
           success: function(msg){$(id).val(msg).removeClass('ac_loading');}
    });   
}
渡你暖光 2024-09-10 05:04:01

使用对象和 for 控制结构:

var fields = {firstname:1, lastname:1, phone:1};
for (field in fields) {
  $.ajax({
    type:"GET",
    url: "autocomplete.asp",
    data: "fld=" + field + "&email=" + request_email,
    beforeSend: function() {
      $('#fld_'+field).addClass('ac_loading');
    },
    success: function(msg) {
      $('#fld'+field).val(msg);
      $('#fld'+field).removeClass('ac_loading');
    }
  });
}

Use an object and the for control structure:

var fields = {firstname:1, lastname:1, phone:1};
for (field in fields) {
  $.ajax({
    type:"GET",
    url: "autocomplete.asp",
    data: "fld=" + field + "&email=" + request_email,
    beforeSend: function() {
      $('#fld_'+field).addClass('ac_loading');
    },
    success: function(msg) {
      $('#fld'+field).val(msg);
      $('#fld'+field).removeClass('ac_loading');
    }
  });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文