//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;
}
}