自动完成动态生成的文本框

问题描述:

我需要使用jquery自动填充来填充动态生成的文本框。自动完成动态生成的文本框

工作流程:

1.On点击添加行按钮,一个行将被插入。

2.1936插入的行,产品文本框应通过自动complete.The同样的方式填补所有动态生成的文本框应自动完成

问题填写:

我已经使用jquery自动完成功能来填充文本框,但自动完成功能仅适用于第一行的文本框。我需要通过自动完成功能填充所有动态创建的文本框。

这是我的代码。

<html> 
<head> 
<script type="text/javascript" src="JS/jquery-1.4.2.min.js"></script> 
<script src="JS/jquery.autocomplete.js"></script> 
<script> 
jQuery(function(){ 
$("#product").autocomplete("Productset.jsp"); 
}); 
</script> 

<script type="text/javascript"> 

     function addRow(tableID) { 

      var table = document.getElementById(tableID); 

      var rowCount = table.rows.length; 
      var row = table.insertRow(rowCount); 

      var colCount = table.rows[0].cells.length; 

      for(var i=0; i<colCount; i++) { 

       var newcell = row.insertCell(i); 

       newcell.innerHTML = table.rows[1].cells[i].innerHTML; 
       //alert(newcell.childNodes); 
       switch(newcell.childNodes[0].type) { 
        case "text": 
          newcell.childNodes[0].value = ""; 
          break; 

        case "select-one": 
          newcell.childNodes[0].selectedIndex = 0; 
          break; 
       } 
      } 
     } 

     function deleteRow(tableID) { 

     try { 

      var table = document.getElementById(tableID); 

      var rowDelete = table.rows.length - 1; 

      if (rowDelete > 1) 

       table.deleteRow(rowDelete); 

      else 

      alert("Cannot delete all the rows.") 
     } 

     catch(e) { 

      alert(e); 
     } 
    } 
    </script> 

</head> 

<body> 
<form> 

     <input type="button" value="Add Row" onclick="addRow('dataTable')" /> 

    <input type="button" value="Delete Row" onclick="deleteRow('dataTable')" /> 

    <br/> 
    <br/> 

    <table id="dataTable" align="center" width="350px" border="1"> 

    <tr> 
     <th> Product Name</th> 
      <th>Quantity</th> 
     <th> Brand</th>  

    </tr> 

    <tr> 

    <td> <input type="text" name="pname" id="product" value="" /></td> &nbsp; 
    <td><input type="text" name="qty" value=""/></td> 
    <td><select name="brand"/> 
     <select> 
      <option value="select">SELECT</option> 

     </select> 
    </td> 
    </table> 
</form> 
</body> 
</html> 

Productset.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@page import="java.sql.*"%> 
<%@page import="java.util.*"%> 

    <% 
    try{ 
    String s[]=null; 

    Class.forName("com.mysql.jdbc.Driver"); 
    Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/pdt","root","root"); 
    Statement st=con.createStatement(); 
    ResultSet rs = st.executeQuery("select distinct product from productlist"); 

     List li = new ArrayList(); 

     while(rs.next()) 
     { 
      li.add(rs.getString(1)); 
     } 

     String[] str = new String[li.size()]; 
     Iterator it = li.iterator(); 

     int i = 0; 
     while(it.hasNext()) 
     { 
      String p = (String)it.next(); 
      str[i] = p; 
      i++; 
     } 

    //jQuery related start 
     String query = (String)request.getParameter("q"); 

     int cnt=1; 
     for(int j=0;j<str.length;j++) 
     { 
      if(str[j].toUpperCase().startsWith(query.toUpperCase())) 
      { 
       out.print(str[j]+"\n"); 
       if(cnt>=5)// 5=How many results have to show while we are typing(auto suggestions) 
       break; 
       cnt++; 
      } 
     } 
    //jQuery related end 

rs.close(); 
st.close(); 
con.close(); 

} 
catch(Exception e){ 
e.printStackTrace(); 
} 


%> 

在我的代码中动态创建文本框不走jQuery自动完成功能。因此,在addrow()方法中包含自动完成功能将使用自动完成数据填充动态创建的文本框。

id选择器将只填充第一个带有自动完成数据的文本框。所以在jquery函数中使用这个$('input[name="product"]').auto complete("Productset.jsp");来填充所有的文本框。

这是完整的代码。

<html> 
<head> 
<script type="text/javascript" src="JS/jquery-1.4.2.min.js"></script> 
<script src="JS/jquery.autocomplete.js"></script> 
<script> 
jQuery(function(){ 
$("#product").autocomplete("Productset.jsp"); 
}); 
</script> 

<script type="text/javascript"> 

     function addRow(tableID) { 

      var table = document.getElementById(tableID); 

      var rowCount = table.rows.length; 
      var row = table.insertRow(rowCount); 

      var colCount = table.rows[0].cells.length; 

      for(var i=0; i<colCount; i++) { 

       var newcell = row.insertCell(i); 

       newcell.innerHTML = table.rows[1].cells[i].innerHTML; 
       //alert(newcell.childNodes); 
       switch(newcell.childNodes[0].type) { 
        case "text": 
          newcell.childNodes[0].value = ""; 
          jQuery(function(){ 
       $('input[name="product"]').autocomplete("Productset.jsp"); 
           }); 

            break; 

        case "select-one": 
          newcell.childNodes[0].selectedIndex = 0; 
          break; 
       } 
      } 
     } 

    </script> 

</head> 

<body> 
<form> 

     <input type="button" value="Add Row" onclick="addRow('dataTable')" /> 

    <input type="button" value="Delete Row" onclick="deleteRow('dataTable')" /> 

    <br/> 
    <br/> 

    <table id="dataTable" align="center" width="350px" border="1"> 

    <tr> 
     <th> Product Name</th> 
      <th>Quantity</th> 
     <th> Brand</th>  

    </tr> 

    <tr> 

    <td> <input type="text" name="product" id="product" value="" /></td> &nbsp; 
    <td><input type="text" name="qty" value=""/></td> 
    <td><select name="brand"/> 
     <select> 
      <option value="select">SELECT</option> 

     </select> 
    </td> 
    </table> 
</form> 
</body> 
</html> 

您需要在添加新行,你应该再次调用自动完成功能调用jQuery的自动完成功能 '开启' 功能

$(document).on("focus","#product",function(e){ 
$(this).autocomplete("Productset.jsp"); 
}); 
+0

它不工作!还有其他解决方案吗? – sound

$("#button").click(function(e) { 
    addRow(); 
    $(".auto").autocomplete({ 
     source: datas 
    }); 
}); 

https://jsfiddle.net/w78L1ho2/

如果您的Productset.jsp没有移动,我建议只调用一次。

为了填补你的DATAS与您的文本文件,你可以做这样的事情 (转换文本文件到阵列来自https://stackoverflow.com/a/6833016/5703316):

var datas = []; 

    function func(data) { 
    datas.push(data); 
    } 

    function readLines(input, func) { 
    var remaining = ''; 

    input.on('data', function(data) { 
     remaining += data; 
     var index = remaining.indexOf('\n'); 
     var last = 0; 
     while (index > -1) { 
     var line = remaining.substring(last, index); 
     last = index + 1; 
     func(line); 
     index = remaining.indexOf('\n', last); 
     } 

     remaining = remaining.substring(last); 
    }); 

    input.on('end', function() { 
     if (remaining.length > 0) { 
     func(remaining); 
     } 
    }); 
    } 

    $.get("Productset.jsp").done(function(result) { 
    readLines(result, func); 
    }); 
+0

该解决方案对于默认数据集合正常工作,但在我的情况下,数据来自数据库。那么如何处理? – sound

+0

您必须执行ajax请求来填写您的列表。有了这样的东西(取决于服务器结果)'$ .get(“Productset.jsp”)。done(function(result){datas = result});' –

+0

除了使用ajax以外,还有其他方法吗?我需要通过使用javascript从数据库(Productset.jsp)获取数据来自动填充文本框 – sound