通过jquery访问表数据(coffeescript)

时间:2013-12-17 19:25:50

标签: javascript jquery coffeescript

我有一张用户表。这是HTML的一行:

<td class=" ">
   <a href="#" data-user="20" data-bb="confirm" class="confirm-user bb-dialog btn btn-success btn-mini">Confirm</a>
</td>

我正在尝试通过此咖啡脚本更新用户记录中的字段:

$(document).ready ->
  userid = null

  $(".confirm-user").click ->
    bootbox.dialog "Are you sure you want to Confirm User?", [
      label: "Confirm"
      class: "btn-success"
      callback: ->
        userid = $(this).data('user')
        alert userid
        $.ajax
          url: "/users/" + userid
          type: "POST"
          data:
            _method: 'PUT'
            user:
              confirmed_at: new Date,
          success: (resp) ->
            window.location.reload()
    ,
      label: "Cancel"
      class: "btn-danger"
      callback: ->
        return 'false'
    ]

但是alert给了我userid

的未定义

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

$(this)的上下文已发生变化。在输入回调之前尝试存储对它的引用:

$(".confirm-user").click ->
  $this = $(this)
  bootbox.dialog "Are you sure you want to Confirm User?", [
  ...
    userid = $this.data('user')