jQuery自动完成功能 - 不确定如何在字段中获取当前文本

时间:2011-10-19 15:15:15

标签: jquery jquery-autocomplete

我目前在自动完成jQuery代码中有类似的东西:

$("input#autocomplete").autocomplete({
    source: "/problems/get_categories_ajax.php?category="+$(this).data("autocomplete", ui.item.autocomplete),
    delay: 0,
    minLength: 0,
    autoFocus: true,
    select: function (event, ui) {
            $("#user_id").val(ui.item.id);
            $(this).data("user_id",ui.item.id);//Store arbitrary data associated with the specified element
            $(this).data("username",ui.item.value);//Store arbitrary data associated with the specified element

    },
        selectFirst: true,
        autoFill: true,
        mustMatch: true
})
.bind("blur",function() {
    var autocomplete = $(this).data("autocomplete");

    $(this).val(autocomplete);
    $("#autocomplete").val(autocomplete);
});    

但是我得到的错误是我在尝试构建AJAX调用时无法使用ui变量:source:“/problems/get_categories_ajax.php?category="+$(this).data("autocomplete” ,ui.item.autocomplete),

如何从表单中获取值以传递到那里?

谢谢!

1 个答案:

答案 0 :(得分:0)

您无需手动将其添加为参数。它将自动附加为term

如果您需要指定其他参数名称,则应声明自定义source函数:

$( "#city" ).autocomplete({
  source: function( request, response ) {
    $.ajax({
      url: "http://gd.geobytes.com/AutoCompleteCity",
      dataType: "jsonp",
      data: {
        q: request.term
      },
      success: function( data ) {
        response( data );
      }
    });
  }
});

(资料来源:http://jqueryui.com/autocomplete/#remote-jsonp