简体   繁体   English

在Rails中,如何在单击“提交”按钮后同时禁用textarea和sumbit按钮?

[英]In Rails, how to disable textarea and sumbit button at the same time after clicking submit button?

I have a form which contains a textarea and a submit button. 我有一个包含文本区域和提交按钮的表单。 When user clicks the submit button, the form will be submitted acyshornizedly using AJAX. 当用户单击“提交”按钮时,将使用AJAX统一提交表单。 I want to disable both the textarea and the button during the submitting. 我想在提交过程中同时禁用textarea和按钮。

I tried binding a JS click event to the submit button and disable these two controls there. 我尝试将JS click事件绑定到“提交”按钮并在那里禁用这两个控件。 My code here: 我的代码在这里:

Form: 形成:

<%= form_for @tweet, remote: true do |f| %>
  <%= hidden_field_tag :authenticity_token, form_authenticity_token %>
    <% if @tweet.errors.any? %>
        <div id="error_explanation">
            <h2><%=pluralize(@tweet.errors.count, "error") %> prohibited this article form being saved:</h2>
            <ul>
                <% @tweet.errors.full_messages.each do |msg| %>
                    <li><%= msg %></li>
                <% end %>
            </ul>
        </div>
    <% end %>

    <div class="div_add_text">
        <%= f.text_area :content, id: "textarea_add_tweet" %>
        <%= f.submit class: "pure-button", id: "btn_add_tweet" %>
    </div>

    <div class="div_submit">
    </div>
<% end %>

JS JS

$(document).ready( function () {
    $("#btn_add_tweet").click(function() {
        $(this).prop("disabled",true);
        $("#textarea_add_tweet").prop("disabled",true);
    });
});

How ever, if i do this, the form would not be submitted. 但是,如果我这样做,将不会提交该表格。 How can I achieve the effect as I want? 如何获得想要的效果?

Have you tried setting a timeout? 您是否尝试过设置超时时间? OnClick gets called before the button's associated action is executed, so disabling asynchronously should work. 在执行按钮的关联操作之前会调用OnClick,因此应该异步禁用。

$(document).ready( function () {
  $("#btn_add_tweet").click(function() {
    setTimeout(function() {
      $("#btn_add_tweet").prop("disabled",true);
      $("#textarea_add_tweet").prop("disabled",true);
    }, 5);
  };
};

Where 5 is the time in milliseconds before the function gets called. 其中5是调用该函数之前的时间(以毫秒为单位)。

I will recommend you to use this: 我建议您使用此:

$(window).submit(function() { $('#btn_add_tweet, #textarea_add_tweet').attr('disabled', true); });

Because if you have added some kind of validation and some how validation fails then it will disable these two things. 因为如果您添加了某种验证以及验证失败的方式,那么它将禁用这两件事。

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

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