admin管理员组文章数量:1287777
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?
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?
Share Improve this question asked May 10, 2016 at 15:52 mr. jmanmr. jman 1213 silver badges15 bronze badges5 Answers
Reset to default 5Consider using a button instead; rails' button helper supports a disabled_with
data
attribute that should automagically disable the button once it's been clicked.
http://api.rubyonrails/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to-label-Data+attributes
This also works for the form submit helper
http://api.rubyonrails/classes/ActionView/Helpers/FormTagHelper.html#method-i-submit_tag-label-Data+attributes
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');
});
<%= link_to "Submit Order", {:action => "charge"}, class: 'btn btn-primary', id: 'confirmButton', data: { disable_with: "Please wait..." } %>
A slight variation to Antarr Byrd's answer
$('a').on('click', '.disabled', function(){
return false;
});
and then add the disabled
class like he mentioned
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. 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. This post is a good example:
disable a hyperlink using jQuery
本文标签: javascriptDisable link after first click in RailsCoffeescriptStack Overflow
版权声明:本文标题:javascript - Disable link after first click in Rails, Coffeescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741329257a2372673.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论