【问题标题】:get the data from SQL and put in jquery从 SQL 中获取数据并放入 jquery
【发布时间】:2015-07-24 10:04:27
【问题描述】:

我正在尝试在 jsp 中执行自动完成文本框。但我不知道如何包含从数据库中检索到的所有数据以用于匹配用户键入的值。请帮助,非常感谢。

  <%
    String FRUIT= "";

    TEST.makeConnection();
    String SQL = "SELECT * FROM TB_FRUIT WITH UR";
    TEST.executeQuery(SQL);     
    while(TEST.getNextQuery())
    {
        FRUIT= TEST.getColumnString("FRUIT_DESCP");
    }   
    TEST.takeDown();    
   %>

  <html>
  <head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
   <script>
    $(function() {
    var availableTags = [
      //how can i include the data(FRUIT) from database to put in here? 

      "Apple", // i hardcode this to do testing for my current autocomplete textbox
      "Avocado", // i hardcode this to do testing for my current autocomplete textbox
      "Banana",// i hardcode this to do testing for my current autocomplete textbox
      "Durian",// i hardcode this to do testing for my current autocomplete textbox
      "Mango",// i hardcode this to do testing for my current autocomplete textbox
    ];
    $( "#tags" ).autocomplete({
        source: availableTags
      });
    });
  </script>      
  <body>
       <div class="ui-widget">
         <label for="tags">Tags: </label>
         <input id="tags">
       </div>
  </body>
  </html>

【问题讨论】:

    标签: jquery jsp autocompletebox


    【解决方案1】:

    我不是 JSP 专家,但我认为我可以在您的代码中发现几个问题:

    1. FRUIT 将始终具有查询中最后一条记录的值,因为您没有连接这些值,而是用新值替换该值。

      您希望FRUIT 是这样的字符串列表:"fruit1","fruit2","fruit3",...,要做到这一点,您应该这样做:

      FRUIT += "\"" + TEST.getColumnString("FRUIT_DESCP") + "\",";
      
    2. 即使您正确获取了值,也不会在网站上显示它。既然您已经以自动完成可以理解的格式获得了 FRUIT 的值,您只需打印它:

      var availableTags = [
          <% out.print(FRUIT); %>
      ];
      

    这应该可以解决问题。

    最后,代码如下所示:

    <%
        String FRUIT= "";
    
        TEST.makeConnection();
        String SQL = "SELECT * FROM TB_FRUIT WITH UR";
        TEST.executeQuery(SQL);     
        while(TEST.getNextQuery())
        {
            FRUIT += "\"" + TEST.getColumnString("FRUIT_DESCP") + "\",";
        }   
        TEST.takeDown();    
       %>
      <html>
      <head>
      <meta charset="utf-8">
      <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
      <link rel="stylesheet" href="/resources/demos/style.css">
       <script>
        $(function() {
        var availableTags = [
            <% out.print(FRUIT); %>
        ];
        $( "#tags" ).autocomplete({
            source: availableTags
          });
        });
      </script>      
      <body>
           <div class="ui-widget">
             <label for="tags">Tags: </label>
             <input id="tags">
           </div>
      </body>
    </html>
    

    【讨论】:

    • 变量 FRUIT 的末尾会有一个不必要的逗号,您应该删除它。你可以这样做:FRUIT = FRUIT.substring(0, FRUIT.length()-1);
    • 谢谢@Alvaro Montoro
    • 嗨@Alvaro Montoro 我可以知道如何解决以下问题吗? stackoverflow.com/questions/31693009/…
    • 问题已删除
    猜你喜欢
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多