<%@ page language="java" contentType="text/html" pageEncoding="utf-8"%> <%@page import="java.util.*" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="content" content="text/html;charset=utf-8"> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript"> function ff() { $.ajax({ type : "post", global : true, url : "servlet/ajaxServer?username=" + document.getElementsByName("username")[0], success : function(data, textStatus) { var testValue = ""; var testKey = ""; if(data != null && data.length != 0){ testKey = data.split("=")[0].split("{")[1]; testValue = data.split("[")[1].split("]")[0]; for(var i = 0; i < testValue.split(",").length; i ++){ //alert(testKey); //alert(testValue.split(",")[i]); //alert("<option label=\"" +testKey + "\" value=\"" + testValue.split(",")[i]+ "\" />"); $("#testName").html("<option label=\"" +testKey + "\" value=\"" + testValue.split(",")[i]+ "\" />"); } } } }); } </script> </head> <BODY> <h1>Automatic prompts</h1> <hr color="red" /> <form method="post" action="./TestJsp"> <input list="username" name="username" id="user" oninput="ff()" /> <datalist id="username"> <div id="testName"></div> </datalist> </form> </BODY> </HTML>
在chrome中应该显示这样的效果,但是没有反映,大家有解决的办法么?谢谢。
url : "servlet/ajaxServer?username=" +document.getElementById("user").value,
var strHTML="<select>";
for(var i = 0; i < testValue.split(",").length; i ++){ //alert(testKey); //alert(testValue.split(",")[i]); //alert("<option label=\"" +testKey + "\" value=\"" + testValue.split(",")[i]+ "\" />");
strHTML=strHTML+"<option label=\"" +testKey + "\" value=\"" + testValue.split(",")[i]+ "\" />");
}
strHTML=strHTML + "</select>"
$("#testName").html=strHTML;
这不是select 标签,是input type="list"