【问题标题】:Simulate a link click in coffeescript在咖啡脚本中模拟链接点击
【发布时间】:2017-10-26 06:24:34
【问题描述】:

我有一个链接,点击后会重定向到聊天页面。现在,我想从咖啡脚本代码中重定向到该页面。怎么模拟下面的链接(带参数)和点击的效果一样?

<%= link_to "Send", "#", class: "chat",
           "data-sender" => current_user.id, "data-receiver" => user.id 
%>

【问题讨论】:

  • 显示你的咖啡脚本文件
  • 在你的链接上触发点击事件怎么样?
  • App.broadcast = App.cable.subscriptions.create "BroadcastChannel",已连接:-> 已断开:-> 已接收:(数据)-> sender_id = data.sender reciever_id = data.receiver $( '.chat').click(发送者:sender_id,接收者:receiver_id)。 . .
  • 嗨,你能告诉我怎么做吗?

标签: javascript ruby-on-rails coffeescript


【解决方案1】:

我认为您首先应该使用评论部分中与您的行动电缆相关的更新来更新问题。

这样的事情应该可以工作。

received: (data) ->
  sender_id = data.sender
  reciever_id = data.receiver
  link = $('a.chat')

  link.attr('data-sender', sender_id)      # To set data attributes for the link
  link.attr('data-receiver', receiver_id)
  link.trigger('click')                    # Triggering a click so that the rest of the code can take over

我不知道您的应用程序是如何工作的。

我正在尝试做的是更新数据属性或链接到从动作电缆获得的值,然后触发链接上的点击事件,以便您的应用程序中的其余代码可以接管。

【讨论】:

  • 这看起来正是我想要的,虽然它仍然不适合我。
  • 会报错吗?你确定received 被解雇了吗?
  • 是的,它被解雇了。如果我注释掉两个“link.data”行,你的代码就可以工作
  • 你的 jQuery 版本是什么?你可以试试link.attr('data-sender', sender_id) 之类的东西,看看它是否有效?
  • 完美!。多谢。我接受了你的回答
猜你喜欢
  • 2011-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-07
  • 1970-01-01
  • 2012-07-26
相关资源
最近更新 更多