javascript – 如何在远程表单上禁用提交按钮,直到加载下一页

前端之家收集整理的这篇文章主要介绍了javascript – 如何在远程表单上禁用提交按钮,直到加载下一页前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在使用Turbolinks的Rails 5.1应用程序中,我在我的提交按钮中添加了一个data-disable-with属性,因此在单击时,该按钮将被禁用,以防止意外多次提交数据.这在许多情况下都很有用.

问题是在使用内置UJS帮助程序(data-remote = true)通过AJAX提交的表单上,单击提交按钮时,它不会保持禁用状态.它最初被禁用,但在下一页加载之前会快速重新启用.这违背了数据禁用行为的要点,因为它使意外的表单重新提交成为可能.

有没有办法在加载新页面之前保持表单按钮被禁用?

这是表格:

  1. <%= simple_form_for(
  2. resource,as: resource_name,url: session_path(resource_name),html: { class: "large",autocomplete: "on" },remote: true
  3. ) do |f| %>
  4. <%= f.input(
  5. :email,placeholder: "Email address",label: false,autofocus: true
  6. ) %>
  7. <%= f.input(:password,placeholder: "Password",label: false) %>
  8. <%= f.button(
  9. :submit,"Sign in",class: "ui fluid large teal submit button","data-disable-with": "Signing in..."
  10. ) %>
  11. <% end %>

这是发生了什么.

解决方法

怎么了?

>表格已提交
> rails-ujs禁用该按钮(data-disable-with behavior)
>表单请求成功
> rails-ujs重新启用按钮
> turbolinks-rails向重定向位置发出请求(可能是一个缓慢的位置,使按钮处于启用状态)

我们需要在第4步之后重新禁用该按钮.为此,我们将监听ajax:success事件,并使用setTimeout禁用它.这确保了在Rails完成它之后它将被禁用. (您可以使用requestAnimationFrame而不是setTimeout,但它不受广泛支持.)

为了防止按钮被缓存在禁用状态,我们将在缓存之前重新启用它. (注意使用一个而不是on来防止before-cache处理程序执行多次.)

我注意到你使用的是jQuery和jquery-ujs,所以我将在下面的代码中使用这些库中的函数.将其包含在主JavaScript文件中的某处.

jQuery的UJS

  1. ;(function () {
  2. var $doc = $(document)
  3.  
  4. $doc.on('submit','form[data-remote=true]',function () {
  5. var $form = $(this)
  6. var $button = $form.find('[data-disable-with]')
  7. if (!$button.length) return
  8.  
  9. $form.on('ajax:complete',function () {
  10. // Use setTimeout to prevent race-condition when Rails re-enables the button
  11. setTimeout(function () {
  12. $.rails.disableFormElement($button)
  13. },0)
  14. })
  15.  
  16. // Prevent button from being cached in disabled state
  17. $doc.one('turbolinks:before-cache',function () {
  18. $.rails.enableFormElement($button)
  19. })
  20. })
  21. })()

rails-ujs / jQuery

  1. ;(function () {
  2. var $doc = $(document)
  3.  
  4. $doc.on('ajax:send',function () {
  5. // Use setTimeout to prevent race-condition when Rails re-enables the button
  6. setTimeout(function () {
  7. $button.each(function () { Rails.disableElement(this) })
  8. },function () {
  9. $button.each(function () { Rails.enableElement(this) })
  10. })
  11. })
  12. })()

rails-ujs / vanilla JS

  1. Rails.delegate(document,'ajax:send',function (event) {
  2. var form = event.target
  3. var buttons = form.querySelectorAll('[data-disable-with]')
  4. if (!buttons.length) return
  5.  
  6. function disableButtons () {
  7. buttons.forEach(function (button) { Rails.disableElement(button) })
  8. }
  9.  
  10. function enableButtons () {
  11. buttons.forEach(function (button) { Rails.enableElement(button) })
  12. }
  13.  
  14. function beforeCache () {
  15. enableButtons()
  16. document.removeEventListener('turbolinks:before-cache',beforeCache)
  17. }
  18.  
  19. form.addEventListener('ajax:complete',function () {
  20. // Use setTimeout to prevent race-condition when Rails re-enables the button
  21. setTimeout(disableButtons,0)
  22. })
  23.  
  24. // Prevent button from being cached in disabled state
  25. document.addEventListener('turbolinks:before-cache',beforeCache)
  26. })

请注意,这将禁用按钮,直到下一页加载所有数据远程表单并带有data-disable-with按钮.您可能希望更改jQuery选择器以仅将此行为添加到选定的表单.

希望有所帮助!

猜你在找的JavaScript相关文章