嗨我在我的项目中有这个代码。我想在按钮点击上验证我的表单然后我想 从同一个按钮单击运行另一个函数(searchdata())。当我运行代码时,它只运行searchdata函数。我需要先运行验证然后运行searchdata函数。 searchdata功能在court.js文件上
这是代码
<?php include('session.php'); ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Release Prisoner</title>
<link rel="stylesheet" type="text/css" href="css/court-home.css">
<link rel="stylesheet" type="text/css" href="css/release.css">
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css">
<script src="js/jquery-2.1.1.min.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/release.js"></script>
<script src="js/court.js"></script>
<script>
$(document).ready(function() {
$("#fm2").validate({
rules:{
look_case_no:"required",
},
messages:{
look_case_no:"enter valid case no",
}
});
$("#search").click(function() {
$("#fm2").valid();
});
});
</script>
</head>
<body>
<div id="wrapper">
<form id="fm2" name="fm2">
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Case No</td>
<td><label>
<input type="text" name="look_case_no" id="look_case_no">
</label></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="button" name="search" id="search" onClick="searchdata();" value="Search"></td>
<td> </td>
</tr>
</table>
</form>
</div>
</body>
</html>
这里是searchdata()
中的court.js
功能:
function searchdata() {
var look_case_no = document.getElementById("look_case_no").value;
// var look_reg_no = document.getElementById("look_reg_no").value;
var url = "court_search_view.php?RequestType=searchview";
url +="&look_case_no="+look_case_no;
//url +="&look_reg_no="+look_reg_no;
htmlobj = $.ajax({
url:url,
async:false
});
document.getElementById("mainDiv").innerHTML = htmlobj.responseText;
var tblMain = document.getElementById("tblMain");
}
答案 0 :(得分:2)
$.valid()
方法检查表单是否有效 AFTER 调用$.validate()方法。换句话说,它没有进行表单验证。
将您的$("#search").click
事件处理程序替换为:
$("#search").click(function() {
$("#fm2").validate();
if($("#fm2").valid())
searchdata();
});