jQuery 自动完成:从“源”中选择 Id;事件

发布于 2024-09-17 05:58:30 字数 1298 浏览 4 评论 0原文

我试图同时将 jQuery UI 自动完成应用于三个输入框,因为请求和逻辑几乎相同,只有一个参数在它们之间发生变化。

由于我使用的是通过 ajax 检索的远程源,因此我试图知道请求是从哪个文本框发出的。

正如您在“source”事件的“switch”语句中看到的那样,我尝试了 $(this).attr("id") 但这不起作用,它返回“indefine”

我尝试了这个,因为它适用于“ select”和“focus”事件,但不在“source”上。我想我使用它的方式是指向“源”

有谁知道一种方法可以知道在这种情况下调用的事件是从哪个元素中调用的?

谢谢!!

$("#campo-categorias, #campo-tipos, #campo-colonias").autocomplete({
    minLength: 1,       
    delay: 100,
    source: function(request, response){
        var solicitud = new Object;

        switch ($(this).attr("id")){
            case "campo-categorias":
                solicitud.action    = 'get_categorias'; 
                break;
            case "campo-tipos":
                solicitud.action    = 'get_tipos_comida';   
                break;
            case "campo-colonias":
                solicitud.action    = 'get_colonias';   
                break;
        }           

        solicitud.consulta  = request.term;                     
        $.ajax({
            url: "wp-admin/admin-ajax.php",
            dataType: "json",
            data: solicitud,
            type: "POST",
            success: function(data){                                        
                response(data);
            }
        });
});

I'm trying to apply jQuery UI autocomplete to three input boxes at the same time since the request and logic is almost the same, only one parameter changes between them.

Since I'm using a remote source that is being retrieved with ajax I'm trying to be able to know from which textbox the request was made.

As you can see in the 'switch' statement at the 'source' event I tried $(this).attr("id") but this doesn't work, it returns 'indefined'

I tried this because it worked on the 'select' and 'focus' events, but not on 'source'. I guess the way I'm using it I'm pointing to the 'source'

Does anyone know a way to know from which element was the event called in this case?

Thanks!!

$("#campo-categorias, #campo-tipos, #campo-colonias").autocomplete({
    minLength: 1,       
    delay: 100,
    source: function(request, response){
        var solicitud = new Object;

        switch ($(this).attr("id")){
            case "campo-categorias":
                solicitud.action    = 'get_categorias'; 
                break;
            case "campo-tipos":
                solicitud.action    = 'get_tipos_comida';   
                break;
            case "campo-colonias":
                solicitud.action    = 'get_colonias';   
                break;
        }           

        solicitud.consulta  = request.term;                     
        $.ajax({
            url: "wp-admin/admin-ajax.php",
            dataType: "json",
            data: solicitud,
            type: "POST",
            success: function(data){                                        
                response(data);
            }
        });
});

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

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

发布评论

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

评论(1

故事灯 2024-09-24 05:58:30

我认为您正在 switch 语句中寻找 this.element.attr("id") 。这应该可以找到正确的 id。

另外,您的代码缺少大括号,它应该如下所示:

 $("#campo-categorias, #campo-tipos, #campo-colonias").autocomplete({
    minLength: 1,       
    delay: 100,
    source: function(request, response){

        var solicitud = new Object;

        switch (this.element.attr("id")){

            case "campo-categorias":
                solicitud.action    = 'get_categorias'; 
                break;
            case "campo-tipos":
                solicitud.action    = 'get_tipos_comida';   
                break;
            case "campo-colonias":
                solicitud.action    = 'get_colonias';   
                break;
        }           

        solicitud.consulta  = request.term;       
        $.ajax({

            url: "wp-admin/admin-ajax.php",
            dataType: "json",
            data: solicitud,
            type: "POST",
            success: function(data){                                        
                response(data);
            }

        });

    }

 });

I think you are looking for this.element.attr("id") in your switch statement. That should work for finding the correct id.

Also, your code was missing a curly bracket, it should look like below:

 $("#campo-categorias, #campo-tipos, #campo-colonias").autocomplete({
    minLength: 1,       
    delay: 100,
    source: function(request, response){

        var solicitud = new Object;

        switch (this.element.attr("id")){

            case "campo-categorias":
                solicitud.action    = 'get_categorias'; 
                break;
            case "campo-tipos":
                solicitud.action    = 'get_tipos_comida';   
                break;
            case "campo-colonias":
                solicitud.action    = 'get_colonias';   
                break;
        }           

        solicitud.consulta  = request.term;       
        $.ajax({

            url: "wp-admin/admin-ajax.php",
            dataType: "json",
            data: solicitud,
            type: "POST",
            success: function(data){                                        
                response(data);
            }

        });

    }

 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文