我正在尝试使用JQuery和coldfusion打印数据库中的信息,以显示学生的信息及其停车许可信息,以及与校园内的汽车相关的其他信息。当我尝试加载网页时,我收到错误,说明在第84行:
<cfquery name="q_sample" datasource="cars_live">
有一个意外的标记:“&lt;”。我猜这是因为它已经在javascript标签下了。有没有办法让JS和coldfusion一起工作,因为为了从数据库中读出我想要的东西,我需要<cfquery name="q_sample" datasource="cars_live">
和$(this).text()
。
以下是学生信息页面的代码。 #plates只是列表的名称,其中包含用户点击的项目,并将其带到此页面。
<div data-role="page" id = 'Student' data-add-back-btn="true">
<div data-role="header">
<h1>Student Info Page</h1>
</div><!-- /header -->
<div data-role="content">
<script type="text/javascript">
$("#plates li").click(function() {
<cfquery name="q_sample" datasource="cars_live">
SELECT FIRST 10 *
FROM veh_rec WHERE LICENSE=$(this).text()
</cfquery>
<cfoutput query="q_sample">
<p>License Plate Number: #license#, <br> Permit ID Number: #decal#, Student ID Number: #ID#</p>
</cfoutput>
});
</script>
</div> <!-- /content -->
</div> <!--/Student -->
如果您需要任何其他信息,请告诉我们!
更新 根据史蒂夫的建议,这是我的新代码。
joey.cfm
<cfparam name="License" default="">
<cfquery name="q_sample" datasource="cars_live">
SELECT * FROM veh_rec WHERE LICENSE=<cfqueryparam cfsqltype="cf_sql_varchar" value="#License#">
</cfquery>
<cfoutput query="q_sample">
<p>License Plate Number: #license# <br> Permit ID Number: #decal#<br> Student ID Number: #ID#</p>
</cfoutput>
html文件的一部分
<div data-role="page" id = 'Student' data-add-back-btn="true">
<div data-role="header">
<h1>Student Info Page</h1>
</div><!-- /header -->
<div data-role="content">
<script type="text/javascript">
$("#plates li").click(function() {
var strLicense=$(this).text();
$.get("joey.cfm", { license: strLicense})
.done(function(data) {
alert("Data Loaded: " + data);
$("#myResults").html(data);
});
});
</script>
<div id="myResults"></div>
现在唯一的问题是我无法获得返回值,除非我在“价值”区域的牌照中进行硬编码。
答案 0 :(得分:7)
此代码不完整,但会让您入门:我这里有2个文件...您的主文件,然后是一个非常简单的文件,用于从服务器检索数据。你可以让cfc以你想要的任何方式处理它,但这会让你开始。
主档案:
<div data-role="page" id = 'Student' data-add-back-btn="true">
<div data-role="header">
<h1>Student Info Page</h1>
</div><!-- /header -->
<div data-role="content">
<div id="result">
</div>
</div> <!-- /content -->
</div> <!--/Student -->
<script type="text/javascript">
$("#plates li").click(function() {
var strLicense=$(this).text();
$.get("getDetails.cfm", { license: strLicense})
.done(function(data) {
alert("Data Loaded: " + data);
$("#result").text(data);
});
});
</script>
GetDetails.cfm
<cfparam name="License" default="">
<cfquery name="q_sample" datasource="cars_live">
SELECT FIRST 10 *
FROM veh_rec WHERE LICENSE=<cfqueryparam cfsqltype="cf_sql_varchar" value="#License#">
</cfquery>
<cfoutput query="q_sample">
<p>License Plate Number: #license#, <br> Permit ID Number: #decal#, Student ID Number: #ID#</p>
</cfoutput>
您的click方法会将您的字符串(我创建一个名为license的变量)传递给jquery,然后.get函数将调用一个新文件getdetails.cfm并将url.variable作为许可证传递。然后,您的查询将运行并将结果传递回.get
函数。