赋值不在外部js文件中

时间:2012-07-03 06:48:15

标签: jquery

朋友们我为自动完成创建了一个新的js文件。除了一行“receivingAutocompleteData = data.autcompleteData;”之外,一切正常。 右侧值未分配到左侧。

var ArrayAutocomplete = [];
var receivingAutocompleteData ;
function AutoComplete1(txtBoxName, idField, fieldName) {
debugger;
ArrayAutocomplete = [];

var selectedReqType = $("#" + txtBoxName).val();

$("#" + txtBoxName).unautocomplete();
$("#" + txtBoxName).val("");

$("#" + txtBoxName).val("Loading...........");
$("#" + txtBoxName).attr("disabled", true);


// var rea=  "<%= Url.RouteUrl(new { controller = "Request", action =   "AutoSelectionDataForRequestType"}) %>";
var rea = getURL(fieldName);
$.post(rea, {}, function (data) {
    debugger;
    receivingAutocompleteData = data.autcompleteData;
    jQuery.each(receivingAutocompleteData, function (i, val) {
        if (val.Name != "")
            ArrayAutocomplete.push(val.Name + '|' + val.ID);
    });
    ArrayAutocomplete.sort();

    //   alert(ArrayAutocomplete.length);
    //  alert(ArrayAutocomplete);
    $("#" + txtBoxName).autocomplete(ArrayAutocomplete, {

        minChars: 0,
        max: ArrayAutocomplete.length,
        autoFill: true,
        mustMatch: true,
        matchContains: false,
        multipleSeparator: ',-',
        scrollHeight: 220,

        formatItem: function (data, index, max) {
            return data[0];
        },
        formatMatch: function (data, index, max) {
            return data[0];
        }, formatResult: function (data, index, max) {
            return data[0];
        }
    }).result(function (event, data, formatted) {
        if (data)
            $("#" + idField).val(data[1]);
        else
            $("#" + idField).val('-1');
    });
    $("#" + txtBoxName).val("");
    $("#" + txtBoxName).attr("disabled", false);
    $("#" + txtBoxName).Watermark("--Select--");

    if (selectedReqType != '')
        $("#" + txtBoxName).val(selectedReqType);
   });

}

2 个答案:

答案 0 :(得分:0)

jQuery使用content-type标头来确定返回数据的类型。由于大多数服务器不发送application/json,因此您必须指定“json”作为数据类型($.post的第4个参数)

你也可能有拼写错误

我认为应该是

data.autocompleteData

而不是

data.autcompleteData

答案 1 :(得分:0)

什么是data

可能你刚刚在receivingAutocompleteData = data.autcompleteData;犯了错误;正确的字段名称是“data.aut o completeData;”。