weixin_33735077 2016-05-20 20:52 采纳率: 0%
浏览 39

ColdFusion结构和Ajax

I have a simple form with a select and a text box. Using the change function, I call a cfc that runs a query and returns a result: right now, as a struct. As odd as this may sound, I need help getting the data out of that struct and into the text box.

Below is what I have for the cfm and the cfc. Any help would be REALLY appreciated.

test.cfm (this is just a simple form)

<cfquery name="getHRSpecialists" datasource="AOO">
    SELECT * FROM [52PrepHRSpecialists]    
</cfquery>

<script src="jquery-2.2.3.min.js"></script>
<script>
$(document).ready(function() {
    $textBox6 = $("#textBox6");
    $("#textBox5").change(function(e) {
        var selected = $(this).val();
        console.log('change:', selected);
        if(selected === '') return;
        $.get("getHRPhone.cfc?method=getPhone", {textBox5:selected}, function(res) {
            $textBox6.html(res);
        });
    });
});
</script>

<cfform>
    <cfselect name="textBox5" id="textBox5" title="Select You Human Resource Specialist's Name" class="headerFields">
       <option value="">Choose a Specialist</option>
       <cfoutput query="getHRSpecialists">
           <option value="#hrSpecName#">#hrSpecName#</option>
       </cfoutput>    
    </cfselect>

    HRS's Phone #: 
    <cfinput id="textBox6" 
           name="textBox6" 
           type="text" 
           title="Your Human Resource Specialist's Phone ##"
           readonly>

</cfform>

getHRPhone.cfc (this is the cfc that gets referenced from the ajax call)

<cfcomponent output="false"> 
    <cffunction name="getPhone" access="remote" output="true" returntype="struct" returnformat="json">
        <cfargument name="textBox5" type="string" required="true" >

        <cfquery name="getPhone" datasource="AOO">
            SELECT hrSpecPhone
            FROM   [52PrepHRSpecialists]
            WHERE  hrSpecName = '#arguments.textBox5#'
        </cfquery>

        <cfset local.obj =  {phone = getPhone.hrSpecPhone} >        

        <cfreturn local.obj>
    </cffunction>
</cfcomponent>

Data (this is the data I get back)

{"PHONE":"123-456-7890"}

All I need is the actual phone number in the input box based on whoever is selected.

  • 写回答

3条回答 默认 最新

  • weixin_33743880 2016-05-21 03:27
    关注

    You're almost there, just need to get the phone number from the JSON object returned.

    replace this:

        $textBox6.html(res);
    

    with this:

        var data = $.parseJSON(res);
        // console.log(data);
        $textBox6.val(data.PHONE);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog