0

我的 HTML 是

  <input type="text" id="id-5505Name" name="id-5505Name" value=" "  class="autocomplete"/> 
  <input type="text" id="id-2333Name" name="id-2333Name" value=" "  class="autocomplete"/>
  <input type="text" id="id-2323Name" name="id-2323Name" value=" "  class="autocomplete"/>
  <input type="text" id="id-2322Name" name="id-2322Name" value=" "  class="autocomplete"/>

我的 JQuery 脚本

   $(".autocomplete").kendoAutoComplete({
        minLength: 2, ignoreCase: true, placeholder: "Start typing...",
        filter: 'contains', suggest: true,
        dataTextField: "name",
        dataValueField: 'id',
        dataSource: {
            type: "json",
            serverFiltering: true,
            transport: {
                read: "PatientFileUpload/loaddrugname", parameterMap: function () { 
                    return {
                        startsWith: $(this).data("kendoAutoComplete").value()
                    };
                },
                success: function (response) {
                    o.success(response);
                }
            }
        }, select: function (e) {
            var item = e.item;
            var DataItem = this.dataItem(e.item.index());
            alert('Name : ' + DataItem.name + ', Id : ' + DataItem.id);
            }
    });

我的问题是:

在前面的代码中,我无法在parameterMap函数中访问$(this)对象,因此我需要访问当前对象,并且我无法更改 JQuery 选择器('.autocompelete')并且我无法使用 Id选择器...任何想法

4

2 回答 2

1

尝试改变:

var DataItem = $(this).dataItem(e.item.index());

至:

var DataItem = this.dataItem(e.item.index());
于 2014-02-15T19:03:56.753 回答
1

使用闭包:

$(".autocomplete").each(function() {
    var that = this;

    $(this).kendoAutoComplete({
       minLength: 2,
       ignoreCase: true,
       placeholder: "Start typing...",
       filter: 'contains',
       suggest: true,
       dataTextField: "name",
       dataValueField: 'id',
       dataSource: {
           type: "json",
           serverFiltering: true,
           transport: {
               read: "PatientFileUpload/loaddrugname",
               parameterMap: function () {
                   return {
                       startsWith: $(that).data("kendoAutoComplete").value()
                   };
               },
               success: function (response) {
                   o.success(response);
               }
           }
       },
       select: function (e) {
           var item = e.item;
           var DataItem = this.dataItem(e.item.index());
           alert('Name : ' + DataItem.name + ', Id : ' + DataItem.id);
       }
   });
});
于 2014-02-15T21:36:56.487 回答