题目一:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>考试题1</title>
<script type="text/javascript" src="css/jquery.js"></script>
</head>
<script type="text/javascript">
$(function(){
$("input[type='button']").click(function(){
//方法1:普通的遍历
//var cBoxs = $("input[type='checkbox']");
//for(var i = 0; i < cBoxs.length; i++){
// if(i == 0 || i == 2 || i ==4){
// cBoxs[i].checked = true;
// }
//}
//方法2:使用each的遍历 $(selector).each(function(index,element))
$.each($("input[type='checkbox']"),function(index,ckb){
if(index == 0 || index == 2 || index == 4)
ckb.checked = true;
});
});
});
</script>
<body>
<input type="checkbox" name="1"/><br/>
<input type="checkbox" name="2"/><br/>
<input type="checkbox" name="3"/><br/>
<input type="checkbox" name="4"/><br/>
<input type="checkbox" name="5"/><br/>
<input type="checkbox" name="6"/><br/>
<input type="button" value="选中1,3,5"/>
</body>
</html>
题目二:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>考试题二</title>
<script type="text/javascript" src="css/jquery.js"></script>
<script type="text/javascript">
$(function(){
$("input[type='submit']").click(function(){
var user = $("input:first").val();
var pwd = $("input:eq(1)").val();
$.ajax({
url:"login.do",
method:"post",
data:{"user":user,"pwd":pwd},
dataType: "text",
async: true,
success:function(msg){
alert(msg);
}
});
});
});
</script>
</head>
<body>
<!-- 要求:将用户名和密码通过jquery传给服务器 -->
<input type="text" name="user"/><br/>
<input type="password" name="pwd"/><br/>
<input type="submit" value="提交"/>
</body>
</html>
package com.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginAction extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
String user = req.getParameter("user");
String pwd = req.getParameter("pwd");
resp.getWriter().print(user+"登录成功"+pwd);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.action.LoginAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
</web-app>