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="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="${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='"+deptid +"'>"+deptName+"</option>")
};
}
});
}
});
});

posted @ 2016-11-11 17:13  sekai  阅读(222)  评论(0编辑  收藏  举报