【问题标题】:using $(this) with coffee script将 $(this) 与咖啡脚本一起使用
【发布时间】:2014-03-21 22:10:26
【问题描述】:

我正在使用查询文件上传。我在页面上有几个表格,除了一个名为“项目”的隐藏字段外,每个表格都相同。这是处理上传的咖啡脚本:

jQuery ->
  $(document).ajaxComplete ->
    $('.project_file').fileupload
      dataType: "script"
      url: ($(this).prev('form').attr("action"))
      paramName: 'file_yo'
      formData: (form) -> 
        [{ name: 'authenticity_token',  value: $('meta[name="csrf-token"]').attr('content')}, { name: 'project',value: $(this).prev().val()}]
      alert($(this).prev().val())
      done: (e, data) ->
        $('#finish-text').html "Upload finished"

这是其中一种形式的 html。从 from 到 form 的唯一变化是名为“project”的隐藏字段的值。

<form accept-charset="UTF-8" action="/projects/6/project_files" class="edit_project" data-remote="true" id="project_file_form" method="post"><div style="margin:0;padding:0;display:inline">        <input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="patch"><input name="authenticity_token" type="hidden" value="iIQXYRJfXRFAaSsvSpb4H+HEUMeAI3pubXDRyd+2Ehk="></div>
    <input id="project" name="project" type="hidden" value="6">
    <input authenticity_token="true" class="field file-field project_file" id="" name="file_yo" title="" type="file" value="">  
</form>

问题是 $(this) 是未定义的。当警报出现时,它显示未定义。如何让 $(this) 返回具有类 'project_file' 的特定元素?

更新:

咖啡脚本编译成以下内容:

(function() {
  jQuery(function() {
    $(document).ajaxComplete(function() {
      return $('.project_file').fileupload({
        dataType: "script",
        url: $(this).prev('form').attr("action"),
        paramName: 'file_yo',
        formData: function(form) {
          return [
            {
              name: 'authenticity_token',
              value: $('meta[name="csrf-token"]').attr('content')
            }, {
              name: 'project',
              value: $(this).prev().val()
            }
          ];
        }
      }, alert($(this).prev().val()));
    });
    return {
      done: function(e, data) {
        return $('#finish-text').html("Upload finished");
      }
    };
  });

}).call(this);

【问题讨论】:

    标签: javascript jquery coffeescript jquery-file-upload jquery-fileupload-rails


    【解决方案1】:

    请记住,当您调用 $('.project_file').fileupload() 时会评估这些选项,因此即使 $(this) 是您所期望的,您也会在每个上传者上设置完全相同的 url。我不认为ajaxComplete 设置任何特定的上下文,所以在回调中的this 本身并不是非常有用。

    如果您想为每个实例提供不同的选项,那么您必须这样说:

    $('.project_file').each ->
      $(@).fileupload
        #... $(@) should be what you want in here
    

    我不知道你为什么将它绑定到ajaxComplete,但乍一看这似乎很奇怪,我希望这将由$(document).ready(...) 直接处理。

    【讨论】:

      【解决方案2】:

      你忘了缩进alert,看看你的代码编译成什么:

      jQuery(function() {
        return $(document).ajaxComplete(function() {
          return $('.project_file').fileupload({
            dataType: "script",
            url: $(this).prev('form').attr("action"),
            paramName: 'file_yo',
            formData: function(form) {
              return [
                {
                  name: 'authenticity_token',
                  value: $('meta[name="csrf-token"]').attr('content')
                }, {
                  name: 'project',
                  value: $(this).prev().val()
                }
              ];
            }
          }, alert($(this).prev().val()), {
            done: function(e, data) {
              return $('#finish-text').html("Upload finished");
            }
          });
        });
      });
      

      【讨论】:

      • 这很奇怪,我的不编译。我将发布我的编译到
      • @Philip7899 变化不大。看看你的alert 在哪里,在那个地方是完全错误的。试试缩进吧,这可能就是你想要的。
      • 我不明白你的意思。它在我的文件中缩进。我尝试用制表符更改空格,但它编译成同样的东西。
      • @Philip7899 我怀疑你希望alert 在你的formData 函数中,如果没有明确指定你需要什么,我就是这种情况添加两个空格。并且:看看你编译的代码,检查你是否认为它是理智的。
      • 你认为this 在ajaxComplete 回调中是什么?
      猜你喜欢
      • 2016-01-05
      • 2016-02-26
      • 2011-11-08
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 2014-07-29
      • 2011-12-01
      • 1970-01-01
      相关资源
      最近更新 更多