我正在使用基本的$ .get()脚本来检索数据库中是否已存在记录。调用的页面返回yes或no,具体取决于是否找到记录。现在,无论如何,它都会返回一个空白值。
这是带有特定输入字段的HTML片段
<p><label for="participant_id">Please enter the Participant ID:</label>
<cfinput type="text" name="participant_id" value="#attributes.stPages.Content.ParticipantId#" required="yes" message="Enter the Participant ID" />
<cfif Len(trim(attributes.stPages.Content.ParticipantId)) eq 0>
<cfinput type="button" name="checkunique" value="Check if Unique" />
<cfinput type="hidden" name="participant_id_valid" value="false" />
</cfif></p>
这是调用脚本
$("#checkunique").click(function() {
$.get('participant_id_check.cfm?participant_id=' + $('#participant_id').val(), function(data) {
result = data.replace(/^\s+|\s+$/g,"");
if (result) {
$('#unique-result').text('Your Participant ID is accepted');
} else {
$('#unique-result').text('Your Participant ID has already been used. Please enter another');
}
});
});
这是调用页面。如果我直接在URL中调用它,它可以正常工作。
<!--- Check the database to ensure that the participant ID entered doesn't already exist (only for new entries). --->
<cfparam name="url.participant_id" default="" type="string" />
<cfquery name="checkID" datasource="#APPLICATION.strConfig.datasource#">
SELECT sessionid
FROM tblsessions
WHERE participant_id = <cfqueryparam cfsqltype="cf_sql_varchar" value="#url.participant_id#" null="no" />
</cfquery>
<!--- No record found; value is unique --->
<cfif Not checkID.RecordCount>
Yes
<cfelse>
No <!--- Record found; value is not unique--->
</cfif>
答案 0 :(得分:1)
您未正确使用$.get
,请尝试以下操作:
$("#checkunique").click(function() {
$.get('participant_id_check.cfm',
{'participant_id': $('#participant_id').val()}, function(data) {
result = data.replace(/^\s+|\s+$/g,"");
if (result) {
$('#unique-result').text('Your Participant ID is accepted');
} else {
$('#unique-result').text('Your Participant ID has already been used. Please enter another');
}
});
});
同样是一个侧面点,你对id='participant_id'
没有任何意见,所以$('#participant_id').val()
可能不会给你任何东西