[英]Why immediately after refreshing my page do I have to click the button with slideToggle on it twice leave the un-toggled form visible?
In my project, I have a button that toggleSlides down a form when it is clicked on. 在我的项目中,我有一个按钮,可以在单击表单时将其向下滑动。 Except in this one circumstance, it works exactly as expected.
除了在这种情况下,它完全按预期工作。
What I expect: When the form is not visible, when the toggling button is clicked the form will appear and remain visible and accessible until either the form is submitted or the button that toggled it visible in the first place is re-clicked making the form not visible. 我的期望:当表单不可见时,单击切换按钮时,表单将显示并保持可见和可访问状态,直到提交表单或重新单击将其切换为可见的按钮为止不可见。
What happens: Immediately after a refresh, if the toggling button is the first button clicked on, the form will toggle visible and slide back up to not be visible without any additional clicks. 发生的情况:刷新后,如果切换按钮是第一个单击的按钮,则该表单将切换为可见状态,然后滑动回去以使其不可见,而无需其他任何单击。
On the server, they both appear to the same request. 在服务器上,它们都出现在相同的请求上。
Immediately after a refresh: 刷新后立即:
Started GET "/users/1/categories/new" for 127.0.0.1 at 2017-01-23 12:09:33 -0500
Processing by CategoriesController#new as JS
Parameters: {"user_id"=>"1"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendering categories/new.js.erb
Rendered categories/_form.html.erb (3.6ms)
Rendered categories/new.js.erb (9.3ms)
Completed 200 OK in 28ms (Views: 20.6ms | ActiveRecord: 0.6ms)
Second click to make the form visible where it remains visible: 第二次单击以使该窗体可见,使其保持可见:
Started GET "/users/1/categories/new" for 127.0.0.1 at 2017-01-23 12:11:22 -0500
Processing by CategoriesController#new as JS
Parameters: {"user_id"=>"1"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendering categories/new.js.erb
Rendered categories/_form.html.erb (2.4ms)
Rendered categories/new.js.erb (5.7ms)
Completed 200 OK in 18ms (Views: 13.1ms | ActiveRecord: 0.4ms)
Below is my relevant code: 以下是我的相关代码:
index.html.erb: index.html.erb:
<div class="row">
<ul id="categories-list"><%= render @categories %></ul>
<div class="row">
<%= link_to new_user_category_path, remote: true do %>
<button>New Category</button>
<% end %>
</div>
<div class="row">
<div id="category-form" style="desplay:none;"></div>
</div>
</div>
CategoriesController: 分类控制器:
...
respond_to :js
def new
@category = current_user.categories.build
end
def create
@category = current_user.categories.create(category_params)
@category.save!
end
...
new.js.erb: new.js.erb:
$('#category-form').html("<%= j (render 'form') %>");
$('#category-form').slideToggle(350);
$('#category_name').focus();
_form.html.erb _form.html.erb
<div id="category-form">
<%= simple_form_for shallow_args(current_user, @category), remote: true do |f| %>
<%= f.input :name %>
<%= f.button :submit, "Add" %>
<% end %>
</div>
create.js.erb: create.js.erb:
$('#categories-list').append("<%= j (render @category) %>");
$('#category-form').slideUp(350);
jQuery is still a little confusing to me. jQuery仍然让我有些困惑。 I appreciate any help you're able to provide explaining what is going on and how to fix it?
感谢您提供的任何帮助,以解释发生的情况以及如何解决? Thanks in advance.
提前致谢。
So in case anyone gets here with the same issue, I had a typo in my index.html.erb
file. 因此,以防万一有人遇到同样的问题,我在
index.html.erb
文件中有一个错字。
<div id="category-form" style="desplay:none;"></div>
Should have been: 本来应该:
<div id="category-form" style="display:none;"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.