首页 > 代码库 > ajax学习
ajax学习
$(function(){
$(":input[name=‘username‘]").change(function(){
var val = $(this).val();
val = $.trim(val);
if(val != ""){
var url="${pageContext.request.contextPath}/validateUserName";
var args = {"username":val,"time":new Date()};
$.post(url,args,function(data){
$("message").html(data);
});
}
});
});
List<String> usernames = Arrays.asList("AAA","BBB","CCC");
String username = request.getParameter("username");
if(username.contains(username)){
result = "<font color=‘red‘>该用户已经被使用。</font>";
}else{
result = "<font color=‘green‘>该用户可以使用。</font>";
}
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.gerWriter().print(result);
---------------------------
java <a href="http://www.mamicode.com/addToCart?id=java&price=100">加入购物车</a>
$(function(){
$("a").click(function(){
var url = this.href;
var args = {"time":new Date()};
$getJSON(url,args,function(date){
$("bookname").text(data.bookname);
});
});
});
java:
后端json格式要是 双引号
json格式:{"bookname":"","number":1}
result.append("{")
.append("/"bookname/":/"" + bookname +"/"")
.append("/"number‘:" + 1 )
.append("}");
response.setContentType("text/javascripte");
response.gerWriter().print(result.toString());
$("#fsfj").hide();
$("fsfs").show();
-----------3级联动
<select id="city">
<option value="">请选择...</option>
<c:forEach items="${locatList}" var="locat">
<option value="http://www.mamicode.com/${locat.id}">${locat.name} </option>
</c:forEach>
</select>
<table id="emppiids" style="display:none">
</table>
$(function(){
$("#city").change(function(){
$("department option:not(:first)").remove();
var city = $(this).val();
if(city != ""){
var url = "eee?eee=aaa";
var args = {"locat":city,"time":new Date()};
$getJSON(url,args,function(data){
if(data.length == 0) {
alert("ddd");
}esle{
for (var i = data.length - 1; i >= 0; i--) {
var deptid = data[i].departmentId;
var deptName = data[i].departmentName;
$("#department").append("<option value=http://www.mamicode.com/‘"+deptid +"‘>"+deptName+"</option>")
};
}
});
}
});
});
ajax学习