0

我想在用户单击文本框时显示供应商名称。为此,我正在使用 jquery.autocomplete.js。我正在关注本教程

我的代码在jsp中

<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <link rel="stylesheet" type="text/css" href="css/jquery.autocomplete.css" />
        <link rel="stylesheet" type="text/css" href="css/jquery-ui.css" />
        <script src="js/jquery.autocomplete.js"></script> 
        <script src="js/jquery-1.9.1.js"></script> 
        <script src="js/jquery-ui.js"></script> 

        <style>
            input {
                font-size: 120%;
            }
        </style>
    </head>
<body>
 <span class="label">Supplier Name</span>
    <span class="ib"> <input type="text" name="supplierId" id="supplierId"/></span>
  <script>
$("#supplierId").autocomplete("autoCompleteSupplier");
 </script>
</body>
</html>

在 struts.xml 中

<action name="autoCompleteSupplier" class="iland.supplier.SupplierAction" method="autoComplete">
            <result name="success">/pages/supplier/suggestion.jsp</result>
            <result name="input">/pages/supplier/suggestion.jsp</result>
            <result name="login">/pages/login.jsp</result>
        </action>

在行动课上

public class SupplierAction extends ActionSupport {
 private String q;//getter and setter method

 public String autoComplete() {

            System.out.println("->SupplierAction autoComplete()");
            System.out.println(getQ());
            SupplierBusiness cb = new SupplierBusiness();
            Map data = cb.autoComplete(getQ());
                setSupplierList((ArrayList) data.get("supplierList"));
                return SUCCESS;
     }
   }

autocompte 应显示以下 jsp 页面

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<s:iterator value="supplierList" var="suplst">
    <s:property value="supplierId"/>
    <s:property value="supplierName"/>
</s:iterator>

使用 crome 检查时显示以下错误

Uncaught SyntaxError: Unexpected end of input jquery.autocomplete.js:462
Uncaught Error: cannot call methods on autocomplete prior to initialization;
attempted to call method 'autoCompleteSupplier' jquery-1.9.1.js:507
4

1 回答 1

0

两件事情:

  1. 将调用autocomplete放在您的 DOM 就绪函数中,例如,在 HTML 的末尾。
  2. 使用source属性来设置动作。使用完整的 URL,例如来自<s:url>标签。
<script>
  $(function() {
    source: "<s:url action='autoCompleteSupplier'/>"
  });
</script>

请注意,此功能包含在Struts 2 jQuery 插件中。

于 2013-10-18T13:41:56.037 回答