自动填充功能无法填充文本框

时间:2015-11-18 10:22:13

标签: java jsp autocomplete

我有自动填充但未填写文本框 我的Java代码:

 String string=request.getParameter("queryString");
 try
 {
  String connectionURL = "jdbc:mysql://localhost:3306/path";
  Connection con;
  Class.forName("com.mysql.jdbc.Driver");
  con = DriverManager.getConnection(connectionURL, "root", ""); 
  String sql = "SELECT name FROM jsample WHERE name LIKE '%"+
                string+"%' LIMIT 10";
  Statement stm = con.createStatement();
  stm.executeQuery(sql);
  ResultSet rs= stm.getResultSet();
  while (rs.next ())
  {
   out.println(
     "<li onclick='fill("+rs.getString("name")+
     ");'>"+rs.getString("name")+"</li>"
   );
  }
 } catch(Exception e) {
   out.println("Exception is ;"+e);
 }

这是我获取值的脚本:

 <head>
  <script type="text/javascript" src="js/jquery.min.js"></script> 
  <script type="text/javascript">
   function lookup(inputString)
   {
    if(inputString.length == 0)
    {
     $('#suggestions').hide();
    } else {
     $.post(
      "states.jsp", 
      {queryString: ""+inputString+""},
      function(data)
      {
       if(data.length >0)
       {
        $('#suggestions').show();
        $('#autoSuggestionsList').html(data);
       }
      });
    }
   }
   function fill(thisValue)
   {
    $('#inputString').val(thisValue);
    setTimeout("$('#suggestions').hide();", 200);
   }
  </script>
  <link rel="stylesheet" href="css/style-au.css"/>
 </head>
 <body>
  <div>
   <form>
    <div>
     <h3><font color="red">Name Selected</states></font></h3><br />
     Enter Name from database to see autocomplete
     <input type="text" size="30" value="" id="inputString" 
      onkeyup="lookup(this.value);" onblur="fill();" />
    </div>
    <div class="suggestionsBox" id="suggestions" style="display: none;">
     <div class="suggestionList" id="autoSuggestionsList"></div>
    </div>
   </form>
  </div>
 </body>

这是我显示自动填充的脚本。

如何将值填充到文本框?

2 个答案:

答案 0 :(得分:0)

因为返回的数据是动态数据,所以您无法捕获此类事件。请参考以下链接 In jQuery, how to attach events to dynamic html elements?

答案 1 :(得分:0)

要改变的三件事:

1。)将ID添加到<li>

out.println("<li onclick='fill("+rs.getString("name")+");' id='"+rs.getString("name")+"' >" + rs.getString("name") + "</li>");

2。)在$('#inputString').val(thisValue.innerHTML);中添加function fill

3。)从onblur="fill();"标记中删除input,不需要。