3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsのremote: trueしたformのsubmitを止める方法

3
Posted at

小技が必要だったので、備忘録としてメモします。

ajax:beforeSendを使います

sample.html.erb
<%= form_tag(
      some_path,
      method: :post,
      remote: true,
      id: 'sample-remote-true-form'
    ) do %>
  <%= text_field_tag 'keyword' %>
  <%= submit_tag '送信' %>
<% end %>

この例ではkeywordが空だったらsubmitを止めます

sample.js
$(document).on('ajax:beforeSend', 'form#sample-remote-true-form', function() {
  if ( ! $(this).find('input[name="keyword"]').val().length > 0 ) {
    return false
  }

})

この情報が誰かのお役に立てば幸いです。

参考

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?