2017-01-02 89 views
0

我想在表單中彈出一個取消按鈕,但單擊取消按鈕時提交表單。取消JQuery驗證表格

如何:從元素中刪除驗證,解除綁定偵聽器,或爲我的表單提交特定事件以便提交,以便在單擊取消按鈕時不提交?去,但是我只是需要

感謝您的幫助

var form = document.createElement('form') 
form.className = 'feedback_form corner_style' 
form.remove = function() {this.parentElement.removeChild(this)} 

var label = document.createElement('label') 
label.innerHTML = 'Why did this not satisfy your need?' 
form.appendChild(label) 
form.appendChild(document.createElement('br')) 

var textarea = document.createElement('textarea') 
textarea.rows = 10 
textarea.cols = 50 
textarea.name = 'feedback' 
form.appendChild(textarea) 
form.appendChild(document.createElement('br')) 

var submit = document.createElement('input') 
submit.innerHTML = 'Submit' 
submit.name = 'submit' 
submit.type = 'submit' 
form.appendChild(submit) 

var cancel = document.createElement('button') 
cancel.innerHTML = 'Cancel' 
cancel.addEventListener('click',() => { 
    console.log('remove form') 
    form.preventDefault() 
    console.log('check 1 ') 
    form.remove() 
}) 
form.appendChild(cancel) 

document.body.appendChild(form) 
$(form).validate({ 
    debug: true, 
    rules: { 
    feedback: 'required' 
    }, 
    submitHandler:() => { 
    console.log('submit not_satisfied') 
    $.ajax({ 
     url: '/not_satisfied', 
     cache: false, 
     data: { 
     id: JSON.parse(row.dataset.extra_columns).id, 
     feedback: 'NotImplimented' 
     }, 
     error: (jqXHR, textStatus, errorThrown) => { 
      console.log(String(jqXHR)) 
      console.log(String(textStatus)) 
      console.log(String(errorThrown)) 
      alert('Error processing request.') 
     }, 
     success: data => { 
     if (! data) alert('Could not complete request.') 
     }, 
     complete:() => {document.dispatchEvent(new Event('only_nest_form_Submit_Complete'))} 
    }) 
    form.remove() 
    } 
}) 

回答