将值绑定到文本框

发布于 2025-01-06 22:32:51 字数 3146 浏览 1 评论 0原文

在使用 jQuery 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>

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

谢谢。

In using jQuery Autosuggest, users are able to start entering a part number and autosuggest shows a list of similar parts. Once a part is select, I want the description to appear in a textbox next to the part number. The part numbers and description are coming back from the CFC, but I am unable to get the part description to appear in the part description text box. how can I do this?

The form with 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#" />

The CFC doing the query:

<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>

Again, once the user successfully selects a part number from the list, I want the associated part description to appear in the partdescription box next to the part number.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原野 2025-01-13 22:32:51

尝试

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

Try

$('#partdescription').val(data['partdescription']);
-柠檬树下少年和吉他 2025-01-13 22:32:51

这最终起作用了:

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

This ended up working:

$('#partdescription').val(ui.item.partdescription);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文