2014-11-20 100 views
2

我想提交一個表單,一旦用戶已經接受他們想通過jQuery UI對話框繼續。jQuery提交'這個'表格

<form method="POST" action="url" onsubmit="return APP.dom.confirm(this);"> 
    <button type="submit" class="btn btn-sm btn-danger"><i class="fa fa-trash"></i></button> 
</form> 

APP.dom.confirm方法是這樣的:

confirm: function(form) { 
    $("#dialog-confirm").dialog({ 
     modal: true, 
     buttons: { 
      "Confirm": function() { 
      $(form).submit(); 
      }, 
      "Cancel": function() { 
      $(this).dialog("close"); 
      } 
     } 
    }); 

    return false; 
} 

這個工作,但是當他們點擊確認我想獲得提交表單。

$(form).submit(); 

這是行不通的。記錄它我得到上面的HTML回來。我試過的變化,都無濟於事:

$(form).clostest('form').submit(); 

如何提交this

回答

5

變化

$(form).submit(); 

form.submit(); 

當你一個jQuery對象調用submit,它要求你再次提交處理程序。直接在DOM元素上調用它不會。

(有趣的是,堆棧片段不讓我提交表單,即使沒有與目標= 「_空白」)

var nesting = 0; 
 
function submitHandler(form) { 
 
    var which = $(form).find("input[type=radio]:checked").val(); 
 
    ++nesting; 
 
    if (nesting > 5) { 
 
    snippet.log("Nested to 5, gave up"); 
 
    } else { 
 
    if (which === "jQuery") { 
 
     snippet.log("Calling via jQuery, nesting = " + nesting); 
 
     $(form).submit(); 
 
    } else { 
 
     snippet.log("Calling via DOM, nesting = " + nesting); 
 
     form.submit(); 
 
    } 
 
    } 
 
    --nesting; 
 
    return false; 
 
}
<form id="the-form" 
 
     onsubmit="return submitHandler(this);" 
 
     action="http://www.google.com/search" 
 
     target="_blank" 
 
     method="GET"> 
 
    <input type="text" name="q" value="kittens"> 
 
    <div> 
 
    <label> 
 
     <input type="radio" name="opts" value="jQuery"> Submit with jQuery 
 
    </label> 
 
    </div> 
 
    <div> 
 
    <label> 
 
     <input type="radio" name="opts" value="DOM"> Submit with DOM 
 
    </label> 
 
    </div> 
 
    <button>Submit</button> 
 
</form> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> 
 
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

+1

Ahhh我明白了,我猜得太過分了。乾杯:) – Alias 2014-11-20 19:17:24

+0

自己也這樣做,很好的回答+1 – 2014-11-20 19:18:14

0
confirm: function() { 
var that = this; 
    $("#dialog-confirm").dialog({ 
     modal: true, 
     buttons: { 
      "Confirm": function() { 
      $(that).submit(); 
      }, 
      "Cancel": function() { 
      $(this).dialog("close"); 
      } 
     } 
    }); 
} 

通過更換onsubmit="return APP.dom.confirm(this);" $('form')。on('submit',APP.dom.confirm);