سؤال

I have the following script inside my razor view :-

$("#Technology2_Tag").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "/Switch/AutoComplete2",
            dataType: "json",
            minLength: 3, delay: 1000,
            data: {
                term: request.term,
                SearchBy: $("#ChoiceTag").prop("checked") ? $("#ChoiceTag").val() : $("#ChoiceName").val(),
            },
            success: function (data) {
                response(data);
            }
        });
    },
});

Currently the autocomplete is firing after typing the first character, instead of firing after typing at least 3 characters as mentioned in the minLength setting. Can anyone advise what might be the problem ?

Thanks

هل كانت مفيدة؟

المحلول

you are putting autocomplete properties in ajax properties, change it like this:

$("#Technology2_Tag").autocomplete({
             minLength: 3, 
             delay: 1000,
            source: function (request, response) {
                $.ajax({
                    url: "/Switch/AutoComplete2",
                    dataType: "json",

                    data: {
                        term: request.term,
                        SearchBy: $("#ChoiceTag").prop("checked") ? $("#ChoiceTag").val() : $("#ChoiceName").val(),
                    },
                    success: function (data) {
                        response(data);
                    }
                });
            }

        });

نصائح أخرى

I think the minLength and delay should be defined outside the definition of source :

$("#Technology2_Tag").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "/Switch/AutoComplete2",
                    dataType: "json",                       
                    data: {
                        term: request.term,
                        SearchBy: $("#ChoiceTag").prop("checked") ? $("#ChoiceTag").val() : $("#ChoiceName").val(),
                    },
                    success: function (data) {
                        response(data);
                    }
                });
            },
             minLength: 3,
             delay: 1000,

        });
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top