将值绑定到文本框

时间:2012-02-17 21:52:42

标签: jquery coldfusion cfc

在使用jQuery Autosuggest时,用户可以开始输入部件号,autosuggest显示类似部件的列表。选择零件后,我希望描述显示在零件号旁边的文本框中。部件号和说明将从CFC返回,但我无法将部件说明显示在部件说明文本框中。我怎么能这样做?

使用jQuery的表单:

<form action="index.cfm?action=reports:part.test"  method="post">
<fieldset>
    <legend>test</legend>
    <p>Start typing a part number.</p>
    <p>
        <label for="partnum">Part Number: </label>
            <input type="text" id="partnum"  name="partnum" />
            <input readonly="readonly" type="text" id="partdescription" name="partdescription" />
    </p>
    <p>
            <input type="submit" name="submit" value="Submit" />
    </p>
</fieldset>
</form>


<cfsavecontent variable="datatables_definitions">
<!-- added by user.list --> 
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
$(document).ready(function(){



        $('#partnum').autocomplete(
            {source: function(request, response) {
                $.ajax({
                    url: "/reports/services/remote/partSuggest.cfc?method=lookUpPart&returnformat=json",
                    dataType: "json",
                    data: {
                      search: request.term,
                      maxRows: 10
                    },
                    success: function(data) {
                      response(data);
                      $('#partdescription').val(partdescription);
                    },

            })
            },
            parse: function(data){
                return $.map(data, function(item) {
                    return { data: item, value: item, result: item };
                });
            }

        });
    });
</SCRIPT>
</cfsavecontent>

<cfhtmlhead text="#datatables_definitions#" />

执行查询的CFC:

<cfcomponent output="false">
    <cffunction name="lookUpPart" access="remote" returntype="any" >
        <cfargument name="search" type="any" required="false" default="">
        <cfargument name="datasource" type="string" required="no" default="myDSN">

        <!--- Define variables --->
        <cfset var data="">
        <cfset var result=ArrayNew(1)>

        <!--- Do search --->
        <cfquery name="getPart" datasource="#arguments.datasource#">
            SELECT top 20 partnum, partdescription
            FROM part
            WHERE partnum LIKE '%#trim(arguments.search)#%'
            ORDER BY partnum
        </cfquery>

        <!--- Build result array --->
        <cfloop query="getPart">
            <cfset returnStruct = StructNew() />
            <cfset returnStruct["partnum"] = partnum />
            <cfset returnStruct["partdescription"] = partdescription />
            <cfset ArrayAppend(result,returnStruct) />
        </cfloop>

        <!--- And return it --->
        <cfreturn serializeJSON(result) />
    </cffunction>
</cfcomponent>

同样,一旦用户成功从列表中选择了一个部件号,我希望相关的部件描述出现在部件号旁边的部分描述框中。

感谢。

2 个答案:

答案 0 :(得分:1)

尝试

$('#partdescription').val(data['partdescription']);

答案 1 :(得分:1)

这最终起作用了:

$('#partdescription').val(ui.item.partdescription);