dsaf32131 2014-11-26 19:11
浏览 63

jQuery UI Autocomplete附加PHP和MySQL

i have a slightly small problem, i believe, i can't find the error...

What i have is a simple form, which is using autocomplete to find stuff in the database, this works for only the FIRST (default) input field.

What i want it to do is to "re-initialize" autocomplete on appended inputs, but it seems that i cant manage it, and i've tried google it all... (alot may say that :) ).

This is the JS-part.

$(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
                });
});

and here's the form :

            <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>

The error should be quite simple, but i cant find it :(

  • 写回答

1条回答 默认 最新

  • douxi4414 2014-11-26 19:39
    关注

    Glad the link helped. I have worked a solution, too. It seems you're close but because i was not incrementing, your second function (the one not working) was failing.

    I have it working here: http://jsfiddle.net/gd7qke4v/2/

    What I did was adjust your basic autocomplete to this:

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

    which ties the autocomplete to the document, looks for .itemname to be active/keydown, then finds the next input with name of unit. Just replace the .val with your .autocomplete(autocomp_opt) and it should be set.

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型