【问题标题】:coffee script not showing ajax status message咖啡脚本未显示 ajax 状态消息
【发布时间】:2013-12-08 01:37:27
【问题描述】:
->
    $('#new_category').bind "ajax:beforeSend", ->
        notification = 'test'
        $('#notification').html notification

以上代码生成的js代码

(function() {
  return $('#new_category').bind("ajax:beforeSend", function() {
    var notification;
    notification = 'test';
    return $('#notification').html(notification);
  });
});

但是带有 id 通知的元素是空的,它不起作用。

【问题讨论】:

  • 你到底想让代码做什么?为什么要设置notification 变量,然后不在任何地方使用它?
  • 编辑了我的问题,检查一下
  • @Seting 查看我的答案,了解使用咖啡脚本准备文档的“正确”方法...

标签: javascript jquery coffeescript


【解决方案1】:

你需要做一些事情:

  1. 在 DOM 准备就绪时运行此代码
  2. 如果您使用的是 jQuery 1.7+,请使用 .on() 代替 .bind()
  3. 正如@cuberto 指出的那样,您需要使用全局ajaxSend 事件

所以你的代码应该是这样的:

$ ->
    $('#new_category').on "ajaxSend", ->
        notification = 'test'
        $('#notification').html notification

【讨论】:

    【解决方案2】:

    您只是在创建一个函数表达式,而不是在任何地方实际运行它。另外,我不确定你从哪里得到那个事件名称? Here is the list of jQuery AJAX events,我想你想要全球 'ajaxSend' 事件。试试这个:

    do ->
      $('#new_category').on 'ajaxSend', ->
        notification = 'test'
        $('#notification').html notification
    

    【讨论】:

      【解决方案3】:

      您是否尝试过使用 document.ready 功能

      $(document).ready ->
          $('#new_category').on "ajaxSend", ->
              notification = 'test'
              $('#notification').html notification
      

      希望对你有帮助

      【讨论】:

      • 不过,这不是咖啡脚本的方式......请参阅我的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      相关资源
      最近更新 更多