dao層service層jsp頁(yè)面下面是JS代碼。<script type="text/javascript">var xmlHttp;//獲得用戶(hù)輸入內(nèi)容的關(guān)聯(lián)信息的函數(shù)function getMoreContents(){ //首先要獲得用戶(hù)的輸入 var content = document.getElementById("keyword"); if(content.value == ""){ clearContent(); return; } //給服務(wù)器發(fā)送用戶(hù)輸入的內(nèi)容,采用AJAX異步發(fā)送數(shù)據(jù),使用一個(gè)對(duì)象,叫做xmlHttp對(duì)象 xmlHttp = createXMLHttp(); //要給服務(wù)器發(fā)送數(shù)據(jù) var url = "SearchServlet?keyword="+escape(content.value); //true表示js腳本會(huì)在send()方法之后繼續(xù)執(zhí)行,而不會(huì)等待來(lái)自服務(wù)器的響應(yīng) xmlHttp.open("GET",url,true); //xmlHttp綁定回調(diào)方法,這個(gè)回調(diào)方法會(huì)在xmlHttp狀態(tài)改變的時(shí)候被調(diào)用,xmlHttp有4中狀態(tài),0到4,4表示完成 xmlHttp.onreadystatechange = callback; xmlHttp.send(null);}//獲得xmlHttp對(duì)象function createXMLHttp(){ //對(duì)于大多數(shù)的瀏覽器都適用的 var xmlHttp; if(window.XMLHttpRequest){ xmlHttp = new XMLHttpRequest(); } //要考慮瀏覽器的兼容性 if(window.ActiveXObject){ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); if(!xmlHttp){ xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } } return xmlHttp;}//回調(diào)函數(shù)function callback(){ if(xmlHttp.readyState==4){//4表示完成 if(xmlHttp.status==200){//200代表服務(wù)器響應(yīng)成功,404代表資源未找到,500代表服務(wù)器內(nèi)部錯(cuò)誤 //交互成功,獲得相應(yīng)的數(shù)據(jù),是文本格式 var result = xmlHttp.responseText; //解析獲得數(shù)據(jù) var json = eval("("+result+")"); for(var i=0;i<json.length;i++){ setContent(json[i].name); //document.write(json[i].name); } //獲得數(shù)據(jù)之后,就可以動(dòng)態(tài)的顯示數(shù)據(jù),把這些數(shù)據(jù)展示到輸入框下面 document.write(json[i].name); } }}//設(shè)置關(guān)聯(lián)數(shù)據(jù)的展示,參數(shù)代表的是服務(wù)器傳遞過(guò)來(lái)的關(guān)聯(lián)數(shù)據(jù)function setContent(contents){ clearContent(); //首先獲得關(guān)聯(lián)數(shù)據(jù)的長(zhǎng)度,以此來(lái)確定生成多少<tr></tr> setLocation(); var size = contents.length; //設(shè)置內(nèi)容 for(var i=0;i<size;i++){ //var nextNode = contents[i];//代表的是json格式數(shù)據(jù)的第i個(gè)元素 var tr = document.createElement("tr"); var td = document.createElement("td"); td.setAttribute("border", "0"); td.setAttribute("bgcolor", "#fffafa"); td.onmouseover = function(){ this.className = 'mouseOver'; }; td.onmouseout = function(){ this.className = 'mouseOut'; }; td.onclick = function(){ //這個(gè)方法實(shí)現(xiàn)的是當(dāng)用鼠標(biāo)點(diǎn)擊一個(gè)關(guān)聯(lián)數(shù)據(jù)時(shí),關(guān)聯(lián)數(shù)據(jù)自動(dòng)設(shè)置為輸入框的數(shù)據(jù) }; //document.write(size); var text = document.createTextNode(contents); td.appendChild(text); tr.appendChild(td); document.getElementById("content_table_body").appendChild(tr); }}//清空之前的數(shù)據(jù)function clearContent(){ var contentTableBody = document.getElementById("content_table_body"); var size = contentTableBody.childNodes.length; for(var i=size-1;i>=0;i--){ contentTableBody.removeChild(contentTableBody.childNodes[i]); } document.getElementById("popDiv").style.border="none";}//當(dāng)輸入框失去焦點(diǎn)的時(shí)候,關(guān)聯(lián)信息清空f(shuō)unction keywordBlur(){ clearContent();}//設(shè)置顯示關(guān)聯(lián)信息的位置function setLocation(){ //關(guān)聯(lián)信息的顯示位置要和輸入框一致 var content = document.getElementById("keyword"); var width = content.offsetWidth;//輸入框的寬度 var left = content["offsetLeft"];//到左邊框的距離 var top = content["offsetTop"]+content.offsetHeight;//到頂部的距離 //獲得顯示數(shù)據(jù)的div var popDiv = document.getElementById("popDiv"); popDiv.style.border = "black 1px solid"; popDiv.style.left = left+"px"; popDiv.style.top = top+"px"; popDiv.style.width = width+"px"; document.getElementById("content_table").style.width = width+"px";}</script>大家看下,我想要把數(shù)據(jù)庫(kù)里的name顯示在搜索框的下面,但是搜索出來(lái)的有一樣的,JSON的格式感覺(jué)沒(méi)怎么對(duì)。
JSON JS JAVA的問(wèn)題
qq_唐子弦_0
2017-03-07 16:00:31