我有一个可以远程和正常使用的表单.
= form_for @comment, html: { class: 'comment-form' }, remote: request.xhr? do |f|
= f.text_area :body
= f.submit
Run Code Online (Sandbox Code Playgroud)
我希望表单仅在bodytextarea具有内容时提交:
$(document).on 'submit', '.comment-form', (e) ->
text = $(this).find('#comment_body').val()
false if text.length < 1
Run Code Online (Sandbox Code Playgroud)
当表单不是Ajax时,该代码有效.但是当它处于远程状态时,它会失败并且表单仍然会提交.为什么?我也尝试过:
if text.length < 1
e.preventDefault()
false
Run Code Online (Sandbox Code Playgroud)
我正在使用带有Turbolinks的Rails 4.
更新:我尝试绑定ajax:beforeSend事件,它仍然提交:
$(document).on 'page:load', '.comment-form', ->
$(this).bind 'ajax:beforeSend', ->
alert "hi"
Run Code Online (Sandbox Code Playgroud)
我看不到警报......