我不確定你需要什麼,但我會給你一個我將如何去做的例子。
動作/ HTML頁面
<html>
<head>
<script type="text/javascript" src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#tid').autocomplete({
source: function(query, response) {
$.ajax({
url: "funcEntity.cfc?method=userSearch&returnformat=json",
dataType: "json",
data: {
searchPhrase: query.term
},
success: function(result) {
$("#someDiv").html(result);
}
});
}
});
</script>
</head>
<body>
<div id="someDiv">
<!--- displays message --->
</div>
<div>
<form name="someForm" method="POST">
<label class="ricRequired">ID<label><br />
<input type="text" id="tid" name="tid" size="30" maxlength="10" class="ricValidate" ric:required="yes" ric:format="integer" />
</form>
</div>
</body>
</html>
功能/ funcEntity.cfc
<cfcomponent>
<cffunction name="userSearch" access="remote">
<cfargument name="searchPhrase" />
<cfquery name="usernameQuery" datasource="yourDatasource">
SELECT username
FROM someTable
WHERE username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#searchPhrase#">
</cfquery>
<cfif usernameQuery.recordCount GT "0">
<cfset var content = "This username already exists.">
<cfelse>
<cfset var content = "This username is available.">
</cfif>
<cfreturn content>
</cffunction>
</cfcomponent>
這不是測試代碼,我在趕時間排序的。我使用了自動完成功能,以便在您輸入時檢查數據庫。
只有在您願意進行頁面加載時才能避免Ajax。 –
我將如何做頁面重新加載並保持已填充的表單域? – user2967577
如果會涉及「POST」變量,然後用它們重新填充輸入字段。大多數情況下,ajax會大大簡化事情。以前你曾使用過它嗎? –