首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

怎么用Ajax做下拉框

2012-01-03 
如何用Ajax做下拉框bodyform methodpost actiongetAlltable border1 aligncenter width

如何用Ajax做下拉框
<body>
  <form method="post" action="getAll">
  <table border="1" align="center" width="50%">
  <tr>
  <td colspan="2"><strong>影院查询与发布系统</strong></td>
  </tr>
 
  <tr>
  <td>电影名称:</td>
  <td><input type="text" name="fN"></td>
  </tr>
 
  <tr>
  <td>电影类型:</td>
  <td>
  <select>
  <option value="0">全部</option>
  </select>
  </td>
  </tr>
 
  <tr>
  <td>主演:</td>
  <td><input type="text" name="fA"></td>
  </tr>
 
  <tr>
  <td>导演:</td>
  <td><input type="text" name="fD"></td>
  </tr>
 
  <tr>
  <td>电影名称:</td>
  <td><input type="text" name="fN"></td>
  </tr>
 
  <tr>
  <td>价格:</td>
  <td><input type="text" name="mi">至<input type="text" name="mx"></td>
  </tr>
 
  <tr>
  <td>查询:</td>
  <td><input type="submit" value="提交查询内容"/></td>
  </tr>
 
  </table>
  </form>
  </body>
我要怎么写方法,来根据我所写的内容来查询。就是说,如果我只输个电影名称,也能查到结果,我全部输人了就能更精确的查找
另外我想电影类型是用AJax来做的求指导,给点提示,不一定要告我代码怎么写

[解决办法]
<%@ page language="java" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>
注册
</title>
<script type="text/javascript">

var xmlHttp; 
function createXMLHttpRequest() {

if (window.XMLHttpRequest) { 
xmlHttp = new XMLHttpRequest(); 
} else if (window.ActiveXObject) { 
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 


if(!xmlHttp){
alert("Error Initializing XMLHttpRequest!");
}
}
 
function UserNameCheck() {

createXMLHttpRequest();

xmlHttp.onreadystatechange = callback;

var username = document.getElementById('username').value;
//var url = "index?user_name=" + username;

var url="user/index";
//xmlHttp.open("get", url, true);
xmlHttp.open("post",url,true);
//xmlHttp.send(null);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send("user_name=" + username);

}


function callback() {



if (xmlHttp.readyState == 4) {

if (xmlHttp.status == 200) {

parseMessage();



// update the HTML DOM based on whether or not message is valid

}else{

alert ("Not able to retrieve description" + xmlHttp.statusText);

} else {
document.getElementById("check_username").innerHTML = "正在验证用户名....";
}

}
 
 
function parseMessage() {
 
//var xmlDoc = xmlHttp.responseXML.documentElement;

//var node = xmlDoc.getElementsByTagName('info');

//document.getElementById('check_username').innerHTML = node[0].firstChild.nodeValue;
document.getElementById('check_username').innerHTML =xmlHttp.responseText;
}

function Form_Submit() {
if(regForm.username.value=="") {
alert("用户名不能为空!");
return false;
} else if (regForm.password.value=="") {
alert("密码不能为空!");
return false;
} else if(regForm.password.value!=regForm.repassword.value) {
alert("两次输入的密码不一致!");
return false;
}
regForm.submit();
}


</script>

</head>


<body>
<div align="center">
<form name="regForm" method="post" action="user/index">
<table width="70%" border="1">
<tr align="center">
<td colspan="2">
用户注册
</td>
</tr>
<tr>
<td width="24%" align="center">
用户名:
</td>
<td width="76%">
<input name="username" type="text" id="username" onblur="UserNameCheck()">
<span id="check_username"></span>
</td>
</tr>
<tr>
<td align="center">
密码:
</td>
<td>
<input name="password" type="password" id="password">

</td>
</tr>
<tr>
<td align="center">
重复密码:
</td>
<td>
<input name="repassword" type="password" id="repassword">

</td>
</tr>
<tr>
<td align="center">
email:
</td>
<td>
<input name="email" type="text" id="email">
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="button" name="Submit" value="按钮" onClick="Form_Submit()">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>


package com.name;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;


public class NameCheck extends ActionSupport implements ServletRequestAware,ServletResponseAware
{

private HttpServletRequest request;
private HttpServletResponse response;
public String[] usernameList;

public void setServletRequest(HttpServletRequest request) {


this.request = request;
}

public void setServletResponse(HttpServletResponse response) {
this.response = response;
}

public String nameCheck() throws IOException{
request.setCharacterEncoding("UTF-8");
response.setContentType("text/xml;charset=UTF-8");

usernameList = new String[] { "Tom", "John", "Hary" };
String username = (String)request.getParameter("user_name");

String rtn = "";
if(username == null ||username.equals("") ){
rtn = "Username is required !";
} else if(this.IsContain(username)){
rtn = "The username has exsites,Please choose other username!";
} else {
rtn = "合法!";
}
PrintWriter out = response.getWriter();
out.print(rtn);
out.flush();
out.close();

return null;
}
public boolean IsContain(String param) {
for (int i = 0; i < usernameList.length; i++) {
if (usernameList[i].equals(param)) {
return true;
} else
continue;
}

return false;
}

}


一个ajax的小例子希望对你有用,用什么做控制层都是一样的 你只要返回一个输出流就行了
[解决办法]

探讨

<%@ page language="java" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";……

[解决办法]
onchang的时候触发你ajax就可以了啊
可以在onload的时候进行你的ajax初始化你的下拉框

热点排行