【问题标题】:jQuery Autocomplete not working for Dynamic Form FieldsjQuery Autocomplete 不适用于动态表单字段
【发布时间】:2011-05-02 11:56:46
【问题描述】:

大家好……我又来了一个问题……

我在表单字段上使用 jquery 自动完成功能。有效!

问题是,然后我动态地在表单中添加另一行,但事实并非如此。是的,我一直将新字段的 id 切换为新字段。

原件 = 已通过 1 动态添加 =exampassed2,exampassed3 等等...

我已经添加了他们的 jQuery 等价物

$("#exampassed1").autocomplete({
    source: "exams.php",
    minLength: 2
});

$("#exampassed2").autocomplete({
    source: "exams.php",
    minLength: 2
});

等等……

我认为问题在于 jQuery 无法识别动态添加的元素,它只读取加载时存在的元素...

有什么方法可以让 jquery 处理程序读取这些新添加的元素??

我听说过 bind 和 live 功能,虽然我找不到实现这些功能的方法...

非常感谢这里的任何小/全部帮助......干杯!

我使用的 jQuery

$().ready(function() {
    $("#autonco").autocomplete({
        source: "nco.php",
        minLength: 2
    });

    $("#autonco1").autocomplete({
        source: "nco.php",
        minLength: 2
    });

    $("#autonco2").autocomplete({
        source: "nco.php",
        minLength: 2
    });

    $("#exampassed1").autocomplete({
        source: "exams.php",
        minLength: 2
    });

    $("#exampassed2").autocomplete({
        source: "exams.php",
        minLength: 2
    });

});

已经存在的代码...

    <td align="center" valign="top">            
    <input type="text" id="exampassed1" name="exam[]" />
    </td>

动态添加的代码...

    <td valign="top" align="center">            
    <input id="exampassed2" name="exam[]" type="text">
    </td>

任何指针也很感激!

再次感谢!

【问题讨论】:

  • 对于最新版本的 jQuery,我会使用 .on() 代替 .live() 来动态添加 html 元素。

标签: php jquery html forms autocomplete


【解决方案1】:

下面是我使用的代码。解决了!

$().ready(function() {

    $("#exampassed1").autocomplete({
        source: "exams.php",
        minLength: 2
    });

    $("#exampassed2").live('focus', function() {

        $("#exampassed2").autocomplete({
        source: "exams.php",
        minLength: 2
        });

    });

});

干杯!

【讨论】:

    【解决方案2】:

    在您收到前一个的回电之前,请勿拨打另一个 $().autocomplete。如果你让它们像那样级联,javascript 引擎将在你从第一个回调之前运行它们。因此它找不到它正在寻找的东西。

    【讨论】:

    • 您好 Jason - 感谢您的提示 - 尽管 - 我该怎么做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多