使用JavaScript / jQuery提交表单后调用函数

时间:2022-04-02 01:18:54

I want to call a function after a form is submitted, I see we can do this in jQuery with .submit(handler function()) but the method description says, the handler method will be executed just before the form is submitted. How can I actually attain this? Should I use setTimeout after the form is submitted or is there any other solution for this?

我希望在提交表单后调用一个函数,我看到我们可以在jQuery中使用.submit(handler function())来执行此操作,但是方法描述说,处理程序方法将在表单提交之前执行。我怎么能真正达到这个目的?我应该在提交表单后使用setTimeout还是有其他解决方案?

3 个解决方案

#1


2  

$("#myFormId").on('submit', function(e) {
    e.preventDefault();
    $.ajax({
        type: $(this).prop('method'),
        url : $(this).prop('action'),
        data: $(this).serialize()
    }).done(function() {
        doYourStuff();
    });
});

#2


0  

You could use plugin http://www.malsup.com/jquery/form/#ajaxSubmit and do what ever you need on the callback method success

您可以使用插件http://www.malsup.com/jquery/form/#ajaxSubmit并在回调方法成功时执行您需要的任何操作

$(document).ready(function() { 
var options = { 
    target:        '#output2',   // target element(s) to be updated with server response 

    success:       function() {
                   // callback code here
                    }

}; 

// bind to the form's submit event 
$('#myForm2').submit(function() { 
    $(this).ajaxSubmit(options); 

    return false; 
}); 
}); 

#3


0  

Flow:

流:

  1. window.sessionStorage['submit'] is initially an empty string.
  2. window.sessionStorage ['submit']最初是一个空字符串。
  3. once you type something in the textbox, and click the submit button, the value in the textbox is saved to window.sessionStorage['submit']. That means the window.sessionStorage['submit'] is not empty anymore. it containes a value.
  4. 一旦在文本框中键入内容,然后单击提交按钮,文本框中的值将保存到window.sessionStorage ['submit']。这意味着window.sessionStorage ['submit']不再是空的。它包含一个价值。
  5. the page refreshes after you submit the form.
  6. 提交表单后页面会刷新。
  7. the onload function will check if there is anything in window.sessionStorage['submit']. if its not empty it means that the form was submitted by a user. then it reset the window.sessionStorage['submit'] to empty string again.
  8. onload函数将检查window.sessionStorage ['submit']中是否有任何内容。如果它不是空的,则意味着表单是由用户提交的。然后它将window.sessionStorage ['submit']重置为空字符串。

Its using Session Storage which is supported in all the major browsers.

它使用所有主流浏览器都支持的会话存储。

<html>
<body onload="checkSubmitStatus()">
<form name="text" action="">
<input type="text" id="txtId">
<input type="submit" value="submit" onclick="storeInSession()"/>
</form>

<script type="text/javascript">
function storeInSession(){
    window.sessionStorage['submit'] = document.getElementById('txtId').value;
}

function checkSubmitStatus(){
    if(window.sessionStorage['submit']){
        alert('The form was submitted by '+ window.sessionStorage['submit']);
        window.sessionStorage['submit'] = '';
    }
}
</script>
</body>

#1


2  

$("#myFormId").on('submit', function(e) {
    e.preventDefault();
    $.ajax({
        type: $(this).prop('method'),
        url : $(this).prop('action'),
        data: $(this).serialize()
    }).done(function() {
        doYourStuff();
    });
});

#2


0  

You could use plugin http://www.malsup.com/jquery/form/#ajaxSubmit and do what ever you need on the callback method success

您可以使用插件http://www.malsup.com/jquery/form/#ajaxSubmit并在回调方法成功时执行您需要的任何操作

$(document).ready(function() { 
var options = { 
    target:        '#output2',   // target element(s) to be updated with server response 

    success:       function() {
                   // callback code here
                    }

}; 

// bind to the form's submit event 
$('#myForm2').submit(function() { 
    $(this).ajaxSubmit(options); 

    return false; 
}); 
}); 

#3


0  

Flow:

流:

  1. window.sessionStorage['submit'] is initially an empty string.
  2. window.sessionStorage ['submit']最初是一个空字符串。
  3. once you type something in the textbox, and click the submit button, the value in the textbox is saved to window.sessionStorage['submit']. That means the window.sessionStorage['submit'] is not empty anymore. it containes a value.
  4. 一旦在文本框中键入内容,然后单击提交按钮,文本框中的值将保存到window.sessionStorage ['submit']。这意味着window.sessionStorage ['submit']不再是空的。它包含一个价值。
  5. the page refreshes after you submit the form.
  6. 提交表单后页面会刷新。
  7. the onload function will check if there is anything in window.sessionStorage['submit']. if its not empty it means that the form was submitted by a user. then it reset the window.sessionStorage['submit'] to empty string again.
  8. onload函数将检查window.sessionStorage ['submit']中是否有任何内容。如果它不是空的,则意味着表单是由用户提交的。然后它将window.sessionStorage ['submit']重置为空字符串。

Its using Session Storage which is supported in all the major browsers.

它使用所有主流浏览器都支持的会话存储。

<html>
<body onload="checkSubmitStatus()">
<form name="text" action="">
<input type="text" id="txtId">
<input type="submit" value="submit" onclick="storeInSession()"/>
</form>

<script type="text/javascript">
function storeInSession(){
    window.sessionStorage['submit'] = document.getElementById('txtId').value;
}

function checkSubmitStatus(){
    if(window.sessionStorage['submit']){
        alert('The form was submitted by '+ window.sessionStorage['submit']);
        window.sessionStorage['submit'] = '';
    }
}
</script>
</body>