jQuery的两个自动完成的文本字段

时间:2018-08-19 09:08:41

标签: javascript php jquery laravel

我不知道为什么如果我只有一个类别的文本字段会自动完成工作(当用户在该类别的文本输入框中键入内容时,我会看到建议的填充),但是当我想使用多个字段时,让我们对子类别说,这两个类别均不起作用(建议不显示)...请帮助

<script type="text/javascript">


        $.getJSON(  {{ route('search.categories') }}, function( data ) {

            var categories = data.map(function(val){
                return val.title;
            });

            auto(categories);

        });
        $.getJSON( {{ route('search.subcategories') }}, function( data ) {

            var subcategories = data.map(function(val){
                return val.title;
            });

            auto(subcategories);

        });

        function auto(categories){

            $("#category_input").autocomplete({
                source: categories,
                minLength: 2
            });
        }
        function auto(subcategories){

            $("#subcategory_input").autocomplete({
                source: subcategories,
                minLength: 2
            });
        }




    </script>

查看:

<input type="text" id="category_input" />
<input type="text" id="subcategory_input"/>

1 个答案:

答案 0 :(得分:1)

尝试一下:

<script type="text/javascript">


    $.getJSON(  {{ route('search.categories') }}, function( data ) {

        var categories = data.map(function(val){
            return val.title;
        });

        auto(categories);

    });
    $.getJSON( {{ route('search.subcategories') }}, function( data ) {

        var subcategories = data.map(function(val){
            return val.title;
        });

        auto_sub(subcategories);

    });

    function auto(categories){

        $("#category_input").autocomplete({
            source: categories,
            minLength: 2
        });
    }
    function auto_sub(subcategories){

        $("#subcategory_input").autocomplete({
            source: subcategories,
            minLength: 2
        });
    }




</script>