如何创建通用(可重用)JavaScript自动完成功能

时间:2010-12-02 22:39:10

标签: javascript generics reusability

我现在有一个有效的JavaScript自动完成功能,感谢你们很多人的帮助。现在我想让函数可重用。需要为函数的每个实例指定三个变量,如下所示。我不知道该怎么做的是用这三个变量的不同值来实例化这个函数。

这是我的HTML字段:

<div class="ui-widget">
    Text or Value:
    <input type="text" id="dotmatch" />
</div>

这是我想要保存在自己的.js文件中的JavaScript代码:

var matchFieldName = 'dotmatch';
var resultFieldName = 'dotnumber';
var lookupURL = "/AutoSuggestJSTest/AutoSuggest.asmx/DOTList";

$(function() {
$('#' + matchFieldName).autocomplete({
    source: function(request, response) {
        $.ajax({
            type: "POST",
            url: lookupURL,
            contentType: 'application/json',
            dataType: "json",
            data: JSON.stringify({ prefixText: request.term, count: 20 }),
            success: function(data) {
                var output = jQuery.parseJSON(data.d);
                response($.map(output, function(item) {
                    return {
                        label: item.Label + "(" + item.Value+ ")",
                        value: item.Value
                    }
                }));
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            }
        });
    },
    minLength: 2,
    select: function(event, ui) {
        $('#' + resultFieldName).val(ui.item.value);
        return ui.item.label;
    }
});

});

2 个答案:

答案 0 :(得分:2)

insin很接近。我今天早上制定的解决方案是;

function AutoComplete(matchFieldName, resultFieldName, lookupURL) {
    $('#' + matchFieldName).autocomplete({
        source: function(request, response) {
            $.ajax({
                type: "POST",
                url: lookupURL,
                contentType: 'application/json',
                dataType: "json",
                data: JSON.stringify({ prefixText: request.term, count: 20 }),
                success: function(data) {
                    var output = jQuery.parseJSON(data.d);
                    response($.map(output, function(item) {
                        return {
                            value: item.Value,
                            label: (item.Label == item.Value) ? item.Label : item.Label + "(" + item.Value + ")"
                        }
                    }));
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        },
        minLength: 2,
        select: function(event, ui) {
            $('#' + resultFieldName).val(ui.item.value);
        }
    });
}

在网页上:

<div id="AutoSuggest">
    DOT Job Title or Number:
    <input type="text" id="dotmatch" style="width:300px;" />
</div>

在网页上,标记之后:

<script type="text/javascript" src="js/DOTAutocomplete.js"></script>

<script type="text/javascript">
    $(function() {
        AutoComplete("dotmatch", "dotnumber", "/AutoSuggestJSTest/AutoSuggest.asmx/DOTList");
    });
</script>

答案 1 :(得分:0)

看起来你正在使用jQuery,所以你可能想要implement it as a plugin

(function($) {
  $.fn.bobsAutocomplete = function(resultFieldName, lookupURL) {
    this.autocomplete({
      source: function(request, response) {
          $.ajax({
              type: "POST",
              url: lookupURL,
              contentType: 'application/json',
              dataType: "json",
              data: JSON.stringify({prefixText: request.term, count: 20}),
              success: function(data) {
                  var output = jQuery.parseJSON(data.d);
                  response($.map(output, function(item) {
                      return {
                          label: item.Label + "(" + item.Value+ ")",
                          value: item.Value
                      }
                  }));
              },
              error: function(XMLHttpRequest, textStatus, errorThrown) {
                  alert(textStatus);
              }
          });
      },
      minLength: 2,
      select: function(event, ui) {
          $('#' + resultFieldName).val(ui.item.value);
          return ui.item.label;
      }
    });
    return this;
  };
})(jQuery);

用法:

$("#dotmatch").bobsAutocomplete("dotnumber", "/AutoSuggestJSTest/AutoSuggest.asmx/DOTList");