2010-02-08 28 views
2

這是jQuery的,無論是game_id和$(本).VAL()爲整數的jQuery + ColdFusion的,ISNUMERIC沒有驗證

/* AJAX call: save to DB input name=#game_id# value=#game_rating */ 
    $.post("game_rating_submit.cfm", { 
     id: game_id, 
     rating: $(this).val() 
    }, 
    function(data) { 
     alert('data: ' + data); 
    }); 

這就是會失敗的ColdFusion的部分:

<cfif NOT IsNumeric("form.rating")> 
    <cfset variables.error = 'Invalid rating value #form.rating#.' > 
</cfif> 
<cfoutput>#variables.error#</cfoutput> 

出於某種原因form.rating不是數字?

回答

6

當引用「form.rating」是字符串「form.rating」時,如果您希望值嘗試...

<cfif NOT IsNumeric(form.rating)> 
    <cfset variables.error = 'Invalid rating value #form.rating#.' > 
</cfif> 
<cfoutput>#variables.error#</cfoutput> 
1

我認爲評價:$(this).val()指的是錯誤的DOM元素。將該引用拉出.post方法外,像這樣。

var rating = $('#my_rating_elem').val(); 
$.post('game_rating_submit.cfm', { id: game_id, rating: rating}) ... 

,或者確保$(這)是指對象,你期望通過將其分配到另一個局部變量

VAR克拉斯= $(本);這是必要的,因爲在.post()方法的上下文中,$(this())是這個必需的,因爲在$ .post()方法中,$(this )不是指您期望的對象(這是$(this)在.post()之外)

+0

啊,謝謝,這是真的 – davidosomething 2010-02-08 20:41:28