【问题标题】:jQuery autocomplete select not triggeredjQuery自动完成选择未触发
【发布时间】:2015-09-28 10:15:23
【问题描述】:

我正在使用 jQuery jquery-1.10.2 和 jQuery ui 1.11.4

这里是自动完成功能:

$("#txtPOI").autocomplete({
    source: function(request, response) {
        $.ajax({
            url: '<?php echo site_url("crowd/get_POIs") ?>',
            data: {cat: selectedCode, q: request.term},
            dataType: "json",
            type: "post",
            success: function(data) {

                response( $.map( data, function( item )
                {
                    return{
                            label: item.title,
                            value: item.title,
                            contentid: item.contentid,
                            latitude: item.latitude,
                            longitude: item.longitude
                        }
                }));

            },
            fail : function ( jqXHR, textStatus, errorThrown ) {
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
            },
            select: function( event, ui ) {
                $("#txtPOI").val(ui.item.latitude);
                alert('selected');
                /*
                alert(ui.item.contentid);
                log( ui.item ?
                  "Selected: " + ui.item.label :
                  "Nothing selected, input was " + this.title);
                alert(ui.item.value);
                */
            },
            open: function(event, ui) {
                $(".ui-autocomplete").css("z-index", 1000);
            },
            minLength: 3

        });
    }
});

ajax 响应成功从数据库返回数据。但是,当我尝试在select 内做某事时,什么也没发生,似乎select 没有被触发。我想在选择自动完成项时获得ui.item.contentid 以及ui.item.latitudeui.item.longitude 的值。

为什么没有触发select 事件?如何解决这个问题?

【问题讨论】:

  • 尝试删除 $("#txtPOI").val(ui.item.latitude);并测试你是否让警报正常工作
  • 它不工作。浏览器调试器上也没有显示错误
  • 你能提供一些html和ajax的url,以便我可以帮助你
  • 检查@Neeraj Verma 答案
  • 是的。它现在正在工作。谢谢

标签: jquery ajax jquery-ui autocomplete


【解决方案1】:

选择是自动完成对象的选项,而不是 Ajax 对象。

试试这个:

$("#txtPOI").autocomplete({
    source: function(request, response) {
        $.ajax({
            url: '<?php echo site_url("crowd/get_POIs") ?>',
            data: {cat: selectedCode, q: request.term},
            dataType: "json",
            type: "post",
            success: function(data) {

                response( $.map( data, function( item )
                {
                    return{
                            label: item.title,
                            value: item.title,
                            contentid: item.contentid,
                            latitude: item.latitude,
                            longitude: item.longitude
                        }
                }));

            },
            fail : function ( jqXHR, textStatus, errorThrown ) {
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
            }
        });
    },
    select: function( event ) {
         // do what ever you want to do...
    }
});

【讨论】:

  • 我现在搞错了。非常感谢您的帮助。它现在正在工作
猜你喜欢
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
  • 2017-02-16
  • 2011-05-13
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多