如何取消发送表单

时间:2018-08-02 19:59:46

标签: javascript forms form-submit

我希望在捕获表单的Submit事件时可以验证编辑器内部是否有内容,所有这些工作正常,但是当我返回false以避免发送表单时,一切正常进行,有什么想法吗?

表格

=form_tag new_answer_path(@item_type, @item_id, @thread_type, @thread_id), method: :post, class:"ui reply form", remote: :true do
  %input{type:"hidden", value:"#{ SecureRandom.uuid }", name:"secureRandom"}

  =hidden_field_tag :message
  #quillEditor.clean.small
  .ui.hidden.divider

  .uk-margin
    %button.uk-button.uk-button-text#send_reply{type:"submit", "data-animation":"ripple"} Enviar respuesta

  :javascript
    createQuill($("input[name=secureRandom]").last().val())

JS函数

function createQuill(secureRandom){
  let parent = $("input[value=" + secureRandom + "]").parent()[0]
  let editor = $(parent).find('#quillEditor')[0]
  let description = $(parent).find("input[name=message]")[0]

  let quill = new Quill(editor, {
    modules: {
      toolbar: [
        ['bold', 'italic', 'underline', 'strike', { 'color': [] }, { 'background': [] }],        // toggled buttons
        [{ 'list': 'ordered'}, { 'list': 'bullet' }, { 'indent': '-1'}, { 'indent': '+1' }, { 'align': [] }],
        ['link', 'image', 'video']                        // formula, image and video implement

      ]
    },
    placeholder: '✍️ Escribe algo...',
    theme: 'snow'
  });

  parent.addEventListener('submit', function(event){
    description.value = JSON.stringify(quill.getContents())

    if($(description).val().replace(/\\/g,'').replace(/"/g, "") == "{ops:[{insert:n}]}"){
      return false
    }
  }.bind(this, description), false)
}

0 个答案:

没有答案