简体   繁体   English

首次单击Rails,Coffeescript后禁用链接

[英]Disable link after first click in Rails, Coffeescript

The following link should be disabled after the first click 首次点击后应禁用以下链接

<%= link_to "Submit Order", {:action => "charge"}, class: 'btn btn-primary', id: 'confirmButton' %>

The following code permanently disables the link: 以下代码永久禁用该链接:

ready = ->

$('#confirmButton').click (e) ->
  e.preventDefault()
  return

$(document).ready(ready)    
$(document).on('page:load', ready) 

How do I have to modify this code, so that the link is active on the first click and then disabled on subsequent clicks? 我该如何修改此代码,以使该链接在第一次单击时处于活动状态,而在随后的单击时处于禁用状态?

Consider using a button instead; 考虑使用按钮代替; rails' button helper supports a disabled_with data attribute that should automagically disable the button once it's been clicked. rails的按钮帮助器支持disabled_with data属性,该属性应在单击按钮后自动禁用它。

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to-label-Data+attributes http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to-label-Data+属性

This also works for the form submit helper 这也适用于表单提交助手

http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-submit_tag-label-Data+attributes http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-submit_tag-label-Data+属性

You can add a disabled class to the button after the first click 您可以在第一次单击后将禁用的类添加到按钮

$('#confimationButton').on('click', function() {
  if($(this).hasClass('disabled'))
    return;
  // do work
  $(this).addClass('disabled');
});

A slight variation to Antarr Byrd 's answer Antarr Byrd的回答略有不同

$('a').on('click', '.disabled', function(){
  return false;
});

and then add the disabled class like he mentioned 然后像他提到的那样添加disabled

<%= link_to "Submit Order", {:action => "charge"}, class: 'btn btn-primary', id: 'confirmButton', data: { disable_with: "Please wait..." } %>

Adding a class is fine if you want to change the display properties (color, etc) but doesn't actually disable the link. 如果要更改显示属性(颜色等),但实际上不禁用链接,则添加类是可以的。 Apparently setting the 'disabled' property doesn't either. 显然,设置“ disabled”属性也不起作用。 The solutions I've seen all have to do with setting the 'click' handler of the link to return false or stop propogation of the click action. 我见过的所有解决方案都与设置链接的“单击”处理程序有关,以返回false或停止传播click操作。 This post is a good example: 这篇文章是一个很好的例子:

disable a hyperlink using jQuery 使用jQuery禁用超链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM