jquery this.bindings [0] .val - 有效但是使用它们的后果

时间:2015-10-09 15:34:41

标签: jquery jquery-ui jquery-ui-autocomplete

jquery:this.bindings [0] .val - 有效,但是它们是使用它的后果吗?看下面的代码示例。有更好的方法吗?

    $("#editAssignee,#assignee").autocomplete({
        source: GetPersonNamesSource,
        minLength: 4,           
       select: function (event, ui) {
       }

    GetPersonNamesSource = function (request, response) {
        //  var personName = $('#editAssignee').val();
        var personName = this.bindings[0].value;
        $.ajax({
            type: "GET",
            url: path + '/api/PersonCED/ByBEMSorName/'  + personName,
            data: personName,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            timeout: 99000,  // 8000= 8seconds, 99000 = 1 Min 39 seconds
            success: function (data) {
                response($.map($.parseJSON(data), function (item) {
                    var LastFirstM = item.LastName + ', ' + item.FirstName + ' ' + item.MiddleName;
                    return {
                        value: item.BEMSID,
                        label: LastFirstM
                        //objPerson: item
                    }
                }))
            } // end success
        }) // end .ajax
    }  // end function

1 个答案:

答案 0 :(得分:1)

source选项回调中访问自动完成小部件值的正确方法是通过请求参数的term属性。

例如:

var GetPersonNamesSource = function (request, response) {
  var personName = request.term;
  $.ajax({
    type: "GET",
    url: '/json',
    data: personName,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    timeout: 99000, // 8000= 8seconds, 99000 = 1 Min 39 seconds
    success: function (data) {
        response($.map($.parseJSON(data), function (item) {
            var LastFirstM = item.LastName + ', ' + item.FirstName + ' ' + item.MiddleName;
            return {
                value: item.BEMSID,
                label: LastFirstM
                //objPerson: item
            }
        }))
    } // end success
  }) // end .ajax
} // end function
$("#editAssignee").autocomplete({
  source: GetPersonNamesSource,
  minLength: 4,
  select: function (event, ui) {}
});

旁注:data选项的值转换为查询字符串,如果不是字符串的话。它附加到GET请求的URL。因此,您可能无需手动将其附加到url