Unable to access JavaScript variables in ColdFusion tags in script

蹲街弑〆低调 提交于 2019-12-13 08:41:30

问题


I was trying to take the selected value from the select tag and pass it to cold fusion tags in jquery as follows.

select tag code:

<select id="selectco">
<cfoutput query="colist">
<option value="#cid#">#coname#</option>
</cfoutput>
</select>

jQuery code:

$(document).ready(function() 
{
    $("#selectco").change(function() 
    {
        var e=document.getElementById("selectco");
        var opt=e.options[e.selectedIndex].value;
         $("#selectst").html("<cfquery name='stlist' datasource='tasks'>
select * from state where cid='"+opt+"'
</cfquery><select id='selectct'><cfoutput query='stlist'><option>#stname#</option></cfoutput>");
    });
});

I was able to take the value to opt variable.But am unable to pass the value to the cfquery tag. Please help me.


回答1:


CFML is parsed on the ColdFusion server; Javascript runs on the client browser. The two never "exist" in the same space.

I recommend you read my blog article describing how CF participates in a request.

What you need to do is to read up on data binding in ColdFusion (or in general), which is fairly well documented, so there's little point in replicating it here.




回答2:


Jquery is executed in client/browser side, while ColdFusion is executed server side.

I have done a lot of implementations of that using a CFC. You can try to fire a $.get() request after every select change event.

Lets say I have a component named "myApplication.cfc" saved in mywebsite/cfc.

myApplication.cfc

<cfcomponent>
    <cffunction name="getstateList" access="remote" returntype="string"
        returnformat="plain">
        <cfargument name="cid" required="no" default="" type="string">

        <cfset var stlist = "">

        <cfquery name="stlist" datasource="tasks">
            SELECT  stateCode 
            FROM    state 
            WHERE   cid = <cfqueryparam value="#arguments.cid#" 
                               cfsqltype="cf_sql_varchar">
         </cfquery>


        <cfreturn stlist.stateCode> 
    </cffunction>
</cfcomponent>

Where the $.get('mywebsite/cfc/myApplication.cfc?method=getstateList&cid='+yourParam). So after that you can use the result and populate your select list.



来源:https://stackoverflow.com/questions/17965937/unable-to-access-javascript-variables-in-coldfusion-tags-in-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!