重新启用用disable_with禁用的链接



如何手动重新启用被Rails的disable_with功能禁用的链接(而不是表单元素)?

对可重新启用链接的调用与表单元素略有不同。它实际上将一个处理程序绑定到click事件,从而阻止其他任何事情的发生。我通过调查jquery ujs库是如何做到这一点的。

要扭转这种效果,只需在jQuery对象上使用enableElement方法:

$.rails.enableElement($('a[data-disable-with]'));


对于Turbolinks,它还有助于监视'page:change'事件而不是window.unload:

$(document).on('page:change', function() {
   $.rails.enableElement($('a[data-disable-with]'));
});

我在这里找到了一个解决方案:

$(window).unload(function() {
  $.rails.enableFormElements($($.rails.formSubmitSelector));
});

Rails已经更新了javascript,不再使用jQuery。

您现在可以使用以下内容重新启用元素(假设您仍在使用jQuery):

var selectors = [Rails.linkDisableSelector, Rails.formEnableSelector].join(', ');
$(selectors).each(function() {
  Rails.enableElement(this);
})

嘿,这很简单,你只需要找到按钮并进行

$button = $('#someId')
$.rails.enableElement($button)
$button.removeAttr('disabled')

基于@DGM解决方案,我最终得到了以下代码:

$.rails.enableFormElements($disabled_button);

其中:

$disabled_buttondata-disable-with禁用的按钮的jQuery对象,可以这样选择:

$disabled_button = $('[data-disable-with]');

好吧,我发现了这项有趣的工作(显然问题只在FF中)set:autocomplete=>'off',现在它可以工作了。或者另一个答案也可以。

参考:https://github.com/rails/jquery-ujs/issues/357

您可以使用jQuery来删除Rails添加到按钮的数据禁用属性:$('#disabledbutton').removeAttr('data-disable-with');

最新更新