dongtao5104 2010-10-29 15:46
浏览 44
已采纳

jQuery自动完成功能不适用于动态表单字段

Hey folks... I am back with another issue...

I am using jquery Autocomplete on a form field. It works!

The issue is, then dynamically I add another row to the form, it doesn't. Yes, I keep toggle the id of the new field to a new one.

Original = exampassed1 Dynamically Added = exampassed2, exampassed3 and so on...

I have already added their jQuery equivalents

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

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

and so on...

I think the problem is that jQuery is not recognizing the dynamically added elements, it only reads the elements present on load...

Is there any way that we can poke the jquery handler to read these new added elements??

I heard of the bind and live functions, albeit I cannot find a way to implement those...

Any little/full help here is much appreciated... cheers!

The jQuery I use

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

});

The code that is already there...

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

The code that is added dynamically...

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

Any pointers are also appreciated!

Many thanks again!

  • 写回答

2条回答 默认 最新

  • duanchun1852 2010-10-31 06:09
    关注

    Below is the code I used. Solved it!

    $().ready(function() {
    
        $("#exampassed1").autocomplete({
            source: "exams.php",
            minLength: 2
        });
    
        $("#exampassed2").live('focus', function() {
    
            $("#exampassed2").autocomplete({
            source: "exams.php",
            minLength: 2
            });
    
        });
    
    });
    

    Cheers!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp