【问题标题】:jQuery + Coldfusion, isNumeric not validatingjQuery + Coldfusion,isNumeric 不验证
【发布时间】:2010-02-08 18:29:14
【问题描述】:

这是 jQuery,game_id 和 $(this).val() 都是整数

    /* 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);
    });

这是失败的冷融合部分:

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

由于某种原因 form.rating 不是数字?

【问题讨论】:

    标签: jquery ajax validation coldfusion


    【解决方案1】:

    当引用“form.rating”是字符串“form.rating”时,如果你想要值试试...

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

    【讨论】:

      【解决方案2】:

      我认为 rating: $(this).val() 指的是错误的 DOM 元素。将该引用拉到 .post 方法之外,就像这样。

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

      或者通过将 $(this) 分配给另一个局部变量来确保 $(this) 是指您期望的对象

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

      这是必要的,因为在 .post() 方法的上下文中,$(this) 不是指您期望的对象(即 .post() 之外的 $(this))

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-08
        • 2010-11-25
        • 2010-09-06
        相关资源
        最近更新 更多