如何同时返回两个"id"和“文本" [英] How to return both an "id" and "text"

查看:85
本文介绍了如何同时返回两个"id"和“文本"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自动完成按名称搜索.我可以输出名称,但是提交表单后,我想传递emp_id值,而不是fullname.如何修改它以获得emp_id值,而不是fullname?

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?

提交表单后,我希望得到emp_id而不是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>

推荐答案

您的意思是显示所选的全名",但将其ID存储在另一个表单字段中吗?

Do you mean display the "FullName" selected, but store its ID in another form field?

添加用于存储"id"的表单字段:

Add a form field for storing the "id":

  <input id="gettingName">
  <input id="theValue">

修改组件以返回结构数组.每个结构包含两个名为值"(id)和标签"(fullName)的键.

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>

在javascript中,将选择事件用于

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; 
},  

这篇关于如何同时返回两个"id"和“文本"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆