ajax学习
Posted sekai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>")
};
}
});
}
});
});
以上是关于ajax学习的主要内容,如果未能解决你的问题,请参考以下文章