2014-11-08 124 views
-2

嗨我在我的項目中有此代碼。我想驗證我的表單按鈕單擊,然後我想 運行另一個函數(searchdata())從同樣的按鈕點擊。當我運行代碼時,它只運行searchdata函數。我需要首先運行驗證,然後運行searchdata函數。 searchdata funtion是在court.js文件jquery表單驗證按鈕單擊,然後發送JavaScript數據

下面是代碼

<?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>&nbsp;</td> 
    <td>&nbsp;</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>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr>  
    <tr> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr> 
    <td><input type="button" name="search" id="search" onClick="searchdata();" value="Search"></td> 
    <td>&nbsp;</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

哪裏是你的方法 'searchdata()':

替換爲您$("#search").click事件處理程序? – 2014-11-08 07:28:59

+0

爲什麼不把它們放在同一個事件處理程序中? – mags 2014-11-08 07:31:46

+0

@Rohit function searchdata() { \t var look_case_no = document.getElementById(「look_case_no」).value; \t // var look_reg_no = document.getElementById(「look_reg_no」).value; \t \t var url =「court_search_view.php?RequestType = searchview」; \t \t url + =「&look_case_no =」+ look_case_no; \t \t // url + =「&look_reg_no =」+ look_reg_no; \t \t \t htmlobj = $。ajax({url:url,async:false}); \t document.getElementById(「mainDiv」)。innerHTML = htmlobj.responseText; \t var tblMain = document.getElementById(「tblMain」); \t \t } – pran 2014-11-08 07:44:28

回答

2

$.valid()方法檢查表單是否有效AFTER調用$.validate()方法。換句話說,它不會進行表單驗證。

$("#search").click(function() { 
    $("#fm2").validate(); 
    if($("#fm2").valid()) 
    searchdata(); 
});