我是 jQuery 新手,并尝试通过示例来学习它在下面的示例中,我尝试使用 jQUery Ajax 在锚标记单击事件上调用 servlet。我的页面中有多个锚标记,我希望为每个标记调用不同的 servlet。如果我使用$('#submit1').click 之类的锚标记 id,如下例所示,它不起作用。但是,如果我用 'a' 替换它,它会为两个锚标记调用相同的 servlet
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>AJAX calls using Jquery in Servlet</title>
<script src="http://code.jquery.com/jquery-latest.js">
</script>
<script>
$(document).ready(function() {
$('#submit1').click(function(event) {
event.preventDefault();
var username=$('#user').val();
$.get('ActionServlet',{user:username},function(responseText) {
$('#welcometext').text(responseText);
});
});
});
$(document).ajaxStart(function(){
$('#content_progress').text("Loading...");
});
$(document).ajaxComplete(function(){
$('#content_progress').text("");
});
</script>
</head>
<body>
<form id="form1">
<h1>AJAX Demo using Jquery in JSP and Servlet</h1>
Enter your Name:
<input type="text" id="user"/><br>
<a name="submit1" href="#">View Profile</a>
<a name="submit2" href="#">Course Details</a>
<br/>
</form>
<div id="content_progress">
</div>
<div id="welcometext">
</div>
</body>
</html>