<pre name="code" class="javascript">
1、表单验证
2、$.post
function save_project(ctx){
alert(121);
/*if(!checkForm()){
return;
}*/
var url="${ctx}/project/ajax/save_project";
var param=$("#proForm").serialize();
param=param+"&random="+Math.random();
alert(param);
$.post(url,param,function(json){
if(!json.flag){
alert("失败");
}else{
alert("成功");
}
});
}
3、$.ajax
function doTestJson(ctx){
alert( $("#proForm").serialize());
$.ajax({
url:ctx+'/project/ajax/save_project',
type: 'POST',
data: $("#proForm").serialize(),
dataType: 'json',
contentType:'application/json;charset=UTF-8',
success: function(json) {
alert(json.result);
}
});
}
//序列化表单元素,返回json数据
var params = $("#userForm").serializeArray();
//也可以把表单之外的元素按照name value的格式存进来
//params.push({name:"hello",value:"man"});
4、form提交
第一种:
$(function() {
$("#tijiao").click(function() {
if(checkForm()){
$("#myForm").ajaxSubmit({
url: "${ctx}/teacher/addTeacher.shtml", // 提交的页面
data: $("#myForm").serialize(), // 从表单中获取数据
type: "POST", // 设置请求类型为"POST",默认为"GET"
dataType:"json",
success: function(json) {
jAlert(json.result, '结果',function(r){
if(json.flag) {
window.close();
}else {
return;
}
});
}
});
}else{
jAlert("请检查表单必填项,是否填写", '结果');
}
return false;
});
});
$(function(){});这个是必须的。
第二种:
//保存所有选中的
$("#myForm2").submit(function() {//重点难点
if(true){
$(this).ajaxSubmit({
url: "${ctx}/courseservices/saveText.shtml",
data: $("#myForm2").serialize(),
type: "POST",
dataType:"json",
success: function(json){
if(!json.flag){
alert('失败');
}else{
alert('成功');
}
}
});
}
return false;
});
5、jquery全选/取消 checkbox
$("#checkall").click(function(){
if(this.checked){
$("input[type='checkbox'][name='indexs']").each(function(){this.checked=true;});
}else{
$("input[type='checkbox'][name='indexs']").each(function(){this.checked=false;});
}
});
6、级联选择
/**
* 组织机构联想查询
* @param name
* @param selectId
* @param deptType
*/
function getDeptByName(name,selectId,deptType){
$.ajax({
url:contextPath+"/public/getDeptByName.shtml",
type:"post",
data:{"deptName":$("#"+name).val(),"deptType":deptType,"r":Math.random()},
dataType:"json",
success:function(msg){
$("#"+selectId).empty();
$('#'+selectId).append("<option value=''>请选择</option>");
$.each(msg.mapList, function(i, item) {
$('#' + selectId).append("<option value='" + item.deptId + "'>"+item.deptName+ "</option>");
});
}
});
}
or
/**
* 场地联动查询
* target 目标name
* */
function getVenuesByAreaId(areaId,target){
$.ajax({
url:contextPath+"/getVenuesByAreaId.shtml",
type:"post",
data:{"id":areaId,"r":Math.random()},
dataType:"json",
success:function(msg){
//alert(msg.gtlist.length);
var option = "";
option +="<option value=''>场地</option>";
for(var i=0;i<msg.mapList.length;i++){
option += "<option value='"+msg.mapList[i].id+"'>"+msg.mapList[i].name+"</option>";
}
$("select[name='"+target+"']").html(option);
}
});
}