开发者

jQuery + Coldfusion, isNumeric not validating

开发者 https://www.devze.com 2022-12-19 07:43 出处:网络
This is the jQuery, both the game_id and the $(this).val() are integers /* AJAX call: save to DB input name=#game_id# v开发者_运维技巧alue=#game_rating */

This is the jQuery, both the game_id and the $(this).val() are integers

    /* AJAX call: save to DB input name=#game_id# v开发者_运维技巧alue=#game_rating */
    $.post("game_rating_submit.cfm", {
        id: game_id,
        rating: $(this).val()
     }, 
     function(data) {
        alert('data: ' + data);
    });

This is the coldfusion part that's failing:

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

For some reason form.rating is not numeric?


When quoted "form.rating" is the string "form.rating", if you want the value try...

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


I think rating: $(this).val() is referring to the wrong DOM element. Pull that reference outside the .post method, like so.

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

Or make sure that $(this) is referring to the object you expect by assigning it to another local variable

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

This is necessary because in the context of the .post() method the $(this) is NOT referring to the object you were expecting (which is the $(this) outside the .post())

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号