jQuery UI Autocomplete append with PHP and MySQL

jQuery UI Autocomplete append with PHP and MySQL

本文关键字:PHP and MySQL with append UI Autocomplete jQuery      更新时间:2023-09-26

我有一个小问题,我相信,我找不到错误...

我有的是一个简单的表单,它使用自动完成功能在数据库中查找内容,这仅适用于 FIRST(默认)输入字段。

我希望它做的是对附加的输入"重新初始化"自动完成,但似乎我无法管理它,我已经尝试了谷歌这一切......(很多人可能会说:))。

这是JS部分。

$(function() {
        var i=0;
        var autocomp_opt={
                source: "functions.php",
                minLength: 3,
            }
                $(".itemname").on("keydown.autocomplete", function() { // this one works
                    $(this).autocomplete(autocomp_opt);
                });
                $(".itemname["+i+"]").on("keydown.autocomplete", function(){ // doesnt work
                    $(this).autocomplete(autocomp_opt);
                });
                $(".addfield").click(function(e){ //on add input button click
                    i++;
                     e.preventDefault();
                    $("#createList").append('<br /><input type="text" name="itemname['+i+']" class="itemname['+i+']" placeholder="Livsmedel" />');//add input box
                });
});

这是表格:

            <form method="post" id="createList" action="create.php" class="pure-form" >
                <input type="text" name="itemname[0]" class="itemname" placeholder="Livsmedel" />
                <input type="text" name="amount" style="display:none" class="amount" placeholder="">
                <input type="text" name="unit" class="unit" />
                <button type="button" class="addfield" />+</button>
                <button type="submit" class="pure-button pure-button-primary" name="insert">Lägg in</button>
            </form>

错误应该很简单,但我找不到它:(

很高兴链接有所帮助。我也有一个解决方案。看起来你很接近,但由于i没有递增,你的第二个函数(不工作的功能)失败了。

我在这里工作:http://jsfiddle.net/gd7qke4v/2/

我所做的是将您的基本autocomplete调整为:

$(document).on("keydown", ".itemname", function () {
    //$(this).autocomplete(autocomp_opt);
    $(this).nextAll('input[name="unit"]').val('query result'); // here instead of val, use your autocomplete().   
});

它将autocomplete与文档联系起来,查找处于活动状态/键控的.itemname,然后查找名为 unit 的下一个输入。只需将.val替换为.autocomplete(autocomp_opt)即可设置。