如何在Rails'之后执行Javascript远程表单`disable_with`功能已经完成?

时间:2014-12-01 23:40:30

标签: javascript ruby-on-rails ajax

我正在使用disable_with,并且无法将按钮文本更改为持久化(因为它被disable_with功能覆盖)。删除, data: { disable_with: '...'后,按钮会按预期更新。

disable_with功能完成后,有没有办法执行操作?

形式:

<%= form_for @user, html: {id: 'follow-form'}, remote: true do |f| %>
    <%= button_tag id: 'follow-btn', data: { disable_with: '...' } do %>
        Follow
    <% end %>
<% end %>

JS:

$('#follow-form').on('ajax:success',function(data, status, xhr){
    if (status.action == 'follow') {
        $('#follow-btn').text('Unfollow');
    } else {
        $('#follow-btn').text('Follow');
    }
}

1 个答案:

答案 0 :(得分:1)

可能没有disable_with和:

$('#follow-form').on('ajax:send',function(data, status, xhr){
    $('#follow-btn').text('Please wait...').attr({disabled: true});
});

当AJAX请求成功时:

$('#follow-form').on('ajax:success',function(data, status, xhr){
    if (status.action == 'follow') {
        $('#follow-btn').text('Unfollow').attr({disabled: false});
    } else {
        $('#follow-btn').text('Follow').attr({disabled: false});
    }
});