Ajax+ajax做的表单验证 2011-12-29 11:37:08
分类: 系统运维
//Ajx实现异步示例,blur实现失去焦点触发
jQuery('#formname').blur(check);
function check(){
alert("开始执行Ajax");
//判断用户是否存在
var formname = jQuery("#formname").val();
if(formname==""){
jQuery('#msgName').html('表单名称不能为空');
jQuery('#msgName').css("color","red");
jQuery('#msgName').css("font-size","2em");
//通过id使save按钮disabled
jQuery('#save').attr("disabled","disabled");
}else{
//alert("formName======"+formname);
//执行action类
var urlimp = '';
jQuery.ajax({
type: "POST",
url: urlimp,
data: "cmd=check&formname="+formname,
success: function(msg){
alert( "Data Saved: " + msg );
if(msg=='exist'){
//已存在(控制css)
jQuery('#save').attr("disabled","disabled");
alert("disable");
jQuery('#msgName').html('该表单名已存在');
alert("disable1");
jQuery('#msgName').css("color","red");
jQuery('#msgName').css("font-size","2em");
//保存按钮不可用
// jQuery('#save').attr("disabled","disabled");
}
if(msg=='unexist'){
//通过
jQuery('#msgName').html('该表单名可用');
jQuery('#msgName').css("color","green");
jQuery('#msgName').css("font-size","2em");
//保存按钮可用
jQuery('#save').removeAttr("disabled");
}
}
});
}
//alert("haha执行到此了");
}
Action类:
package com.ext.portlet.song.action;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.ext.portlet.song.model.FormNameBean80425986;
import com.ext.portlet.song.service.FormNameBean80425986LocalServiceUtil;
import com.liferay.portal.struts.AJAXAction;
public class AjaxAction extends AJAXAction {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm arg1,
HttpServletRequest requst, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
System.out.println("AjaxxAction.execute()");
String formname = requst.getParameter("formname");
System.out.println("formName++++++++++++"+formname);
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//通过表单名查找
List list=FormNameBean80425986LocalServiceUtil.findByformname(formname);
if(list==null||list.isEmpty())
{
out.print("unexist");
return null;
}else{
FormNameBean80425986 formbean=(FormNameBean80425986) list.get(0);
out.print("exist");
}
System.out.println("list"+list.size());
//得到list的第一个值
//System.out.println("formbean"+formbean.getFormname()+"_________________________");
//FormNameBean80425986 formbean = FormNameBean80425986LocalServiceUtil.findByformname(formname);
out.flush();
out.close();
return null;
//return super.execute(arg0, arg1, arg2, arg3);
}
@Override
public String getText(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub
return null;
}
}
阅读(3291) | 评论(0) | 转发(0) |
0
上一篇:简单Struts Portlet开发( 提供入门程序源码)
下一篇:js实现对table的增删改查操作
相关热门文章
给主人留下些什么吧!~~
评论热议
请登录后评论。
登录 注册