【问题标题】:Getting an error in jQuery when I try to autosave a form field with an ajax call当我尝试使用 ajax 调用自动保存表单字段时,jQuery 中出现错误
【发布时间】:2011-02-06 02:39:52
【问题描述】:

我正在尝试让 rails 3 项目中的表单字段在更改时自动保存。我正在尝试使用以下 jQuery ajax 调用来做到这一点:

$j("#list_" + <%= item.list_id.to_s %> + "_item_" + <%=item.id.to_s %>).live("change", 
 function(){
      $j.ajax({ 
           beforeSend: function(request) {
               request.setRequestHeader("Accept", "text/javascript"); 
           },
           type: 'POST',
           url: '<%= list_text_item_path(List.find(item.list_id), TextItem.find(item.id)) %>/update',
           data: {
                value: $j(this).val(),
                id: '<%= item.id.to_s %>',
                list_id: '<%= item.list_id.to_s %>'
           },
           success: function(){
                alert("success");
           } 
      });
 });

每当更改文本框时,我都会在 jQuery 中收到以下错误:

 (c.value || "").replace is not a function

这在 jQuery-1.5.min.js 中都有。 jQuery-1.4.2.min.js 也发生了同样的错误。 (我更新到 1.5 看看是否有帮助。)

对我哪里出错有任何想法吗? 非常感谢。

编辑:哦,废话,当我发布我的 HTML 时,我意识到我将 .live() 调用附加到表单输入元素的容器 div 而不是表单输入元素本身。我有一个单独的问题,但我想我会在找出可能导致它的原因后发布...

【问题讨论】:

  • 你能展示你的html的sn-p和渲染完成的js代码吗?
  • 您是否有此错误的回溯,例如来自 Firebug?我最好的猜测是错误中的c 是您作为data: 传递给ajax() 的哈希值,而$j(this).val() 不知何故没有给您一个字符串(!?),因此.replace() 不是其中之一它的方法。但是如果不看到导致该错误的函数调用,就很难说。

标签: jquery ruby-on-rails ajax ruby-on-rails-3 forms


【解决方案1】:

呃,我想通了……一个粗心的错误。我在 HTML 列表项中有输入元素,并且我将 .live() 函数调用附加到 li id 而不是输入元素 id。感谢楼上各位的帮助。

【讨论】:

    猜你喜欢
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多