简体   繁体   English

为什么刷新页面后立即必须单击带有slideToggle的按钮两次才能使未切换的表单可见?

[英]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.

相关问题 为什么我必须单击按钮两次才能加载图像? - Why do I have to click the button twice to have the image loaded? 为什么我必须单击提交按钮 TWICE 才能更新我的 useState() 挂钩? - Why do I have to click Submit button TWICE in order to update my useState() hook? 为什么必须两次单击此输入按钮才能调用函数? - Why do I have to click this input button twice to call a function? 为什么我必须在事件触发之前单击两次按钮? - Why do I have to click button twice before event fires? 提交表单后,为什么我的页面会刷新? - Why is my page refreshing after submitting a form? 为什么在刷新浏览器后立即在文本字段上单击两次以调用Java Script中的函数? - Why do I need to click twice on textfield to invoke the function in Java Script right after refreshing the browser? 单击按钮后如何在不刷新页面的情况下显示本地存储的内容? - How do I display the contents of local Storage after button click without refreshing the page? 在这种情况下,为什么必须单击两次才能加载我的图像? - Why do I have to click twice to have my image loaded in this case? 为什么我的角度单选按钮未标记为已选中(我必须单击两次) - Why isn't my angular radio button marked as selected ( i have to click twice) 在运行js之前,我必须在提交按钮上单击两次。 为什么? - I have to click twice on my submit button before js run. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM