<div class="form-group">
<label class="control-label col-md-3">机号</label>
<div class="col-md-4">
<input class="form-control" id="mask_jh" type="text" name="faultMonitor.jh" onblur="checkJh()"/>
<div id="checkJh" style="float:left"></div>
</div>
</div>HTML的主要代码如上
function checkJh(){
var jh = document.getElementById("mask_jh").value;
var checkJh = document.getElementById("checkJh");
//alert(jh);
$.ajax({
url:"checkJhFaultMonitorAction",
async:true,
type:"POST",
data:{"jh":jh},
success:function(data){
//alert(data.jh);
if(data.jh!=0){checkJh.innerHTML="";}
else{
$("#checkJh").html("未在数据库中的机号");
$("#checkJh").css("color","red");
}
},
error:function(){
//alert("请求失败");
},
dataType:"json"
});
}主要的js代码
public String checkJh() throws IOException {
HttpServletRequest request = ServletActionContext.getRequest();//获取request对象
request.setCharacterEncoding("UTF-8");
HttpServletResponse response = ServletActionContext.getResponse()//获取response对象
response.setContentType("text/html;charset=UTF-8");
jh = request.getParameter("jh");
List<AircraftInfo> aircraftList= new ArrayList<AircraftInfo>();
AircraftInfo acInfo = aircraftInfoService.findById(jh);
if(acInfo!=null)
aircraftList.add(acInfo);
int size = aircraftList.size();
if(aircraftList.size()>0){
response.getWriter().write("{\"jh\":"+size+"}");
}
else{response.getWriter().write("{\"jh\":"+0+"}");}
return NONE;
}Action中的主要代码
本文介绍了一个基于HTML和JavaScript的机号验证系统,通过Ajax调用后台Action进行机号的有效性检查。当输入机号并失去焦点时,前端将触发验证过程,与数据库中的记录进行比对。

391

被折叠的 条评论
为什么被折叠?



