jquery自动完成动态生成的文本框 [英] jquery auto complete for dynamically generated textboxes

查看:22
本文介绍了jquery自动完成动态生成的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 jquery 的新手,我正在处理一个需要使用 自动完成 工具动态生成文本框的网页.

i am new to jquery, i am working on a web page which needs generating text boxes dynamically with autocomplete facility.

我在一些静态内容上测试了 $("#some").autocomplete(data);,效果很好.

i tested $("#some").autocomplete(data); on some static content, it worked perfectly.

但是,当我对动态生成的文本框尝试相同的技术时,它不起作用!

however, when i try the same technique with dynamically generated text boxes it's not working!

我的代码如下:

$(function() {  

  $("#button_newproduct").click(function(){  
    $("#products_table > tbody").append(
      "<tr><td><input type='text'name='td_products["+counter+"]'/></td></tr>");
  });
  var data = "Core celectors cttributes craversing canipulation CSS cvents cffects cjax ctilities".split(" ");
  $('input[name^=td_products]').autocomplete(data);
});

谢谢各位,在你们的帮助下我已经完成了.

thanks guys i am done with this with ur help.

现在,另一个问题.我正在使用 DWR 调用加载数组(输入到自动完成).如下

now, another problem. i am loading the array(input to autocomplete) with a DWR call.as below

DwrService.populateProducts(someFunc);
function someFunc(result){
    autoProducts=result;
    input.autocomplete(result);
 }

这里的问题是每次对 DB 进行 DWR 调用以获取数组!

here problem is everytime making a DWR call to DB to get the array!

有没有办法将 DWR 中的数组存储在全局变量中?

is there any way to store the array from DWR in a global variable?

问候

推荐答案

我认为的主要问题是您在点击处理程序之外调用自动完成.因此,自动完成在页面加载时设置,而不是在单击按钮时设置.

The main issue i think is that you are calling the autocomplete outside of the click handler. So the autocompletion gets set up when the page loads, rather than when the button is clicked.

要解决此问题,请将代码更改为:

To resolve this, alter the code to read:

$(function() {

    $("#button_newproduct").click(function() {

        var newItem = $("<tr><td><input type='text'name='td_products["+counter+"]'/></td></tr>");

        $("#products_table > tbody").append(newItem); 

        var data = "Core celectors cttributes craversing canipulation CSS cvents cffects cjax ctilities".split(" "); 
        newItem.find('input').autocomplete(data);

    });
});

现在自动完成是在每个新项目上设置的,而不是在开始时设置一次.

Now the autocompletion is being set on each new item, rather than once, at the start.

这篇关于jquery自动完成动态生成的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆