问题
I am using AutoComplete to search by name. I'm able to output the names, but after submitting the form, I would like to pass the emp_id
value, not the fullname
. How can I modify it to get the emp_id
value, not the fullname
?
I would like it after I submit the form I get the emp_id
not the fullname
value.
search_employee.cfc
<cfcomponent>
<cffunction name="queryNames" access="remote">
<cfargument name="searchPhrase" />
<cfquery name="query_names" datasource="">
SELECT fullname ,emp_id ....
</cfquery>
<cfset result = arrayNew(1) />
<cfloop query="query_names">
<cfset ArrayAppend(result, query_names.fullname) />
</cfloop>
<cfreturn result />
</cffunction>
</cfcomponent>
JavaScript
<script>
$(document).ready(function() {
$('.gettingName').autocomplete({
source: function(query, response) {
$.ajax({
url: "test.cfc?method=queryNames&returnformat=json",
dataType: "json",
data: {
searchPhrase: query.term
},
success: function(result) {
response(result);
}
});
}
});
</script>
回答1:
Do you mean display the "FullName" selected, but store its ID in another form field?
Add a form field for storing the "id":
<input id="gettingName">
<input id="theValue">
Modify the component to return an array of structures. Each structure containing two keys named "value" (id) and "label" (fullName).
<cfloop query="local.query_names">
<cfset local.data = {"value": emp_id, "label": fullName}>
<cfset ArrayAppend(local.result, local.data) />
</cfloop>
In the javascript, use the select event to update the values of the two fields.
select: function(event, ui) {
$('#gettingName').val(ui.item.label);
$('#theValue').val(ui.item.value);
return false;
},
来源:https://stackoverflow.com/questions/47722031/how-to-return-both-an-id-and-text