Periodically autosave form

后端 未结 5 2411
别那么骄傲
别那么骄傲 2020-12-02 21:44

How to implement a periodical save of a form in the background? Same kinda thing that gmail does.

5条回答
  •  时光说笑
    2020-12-02 22:01

    setInterval(function(){
      var form = $('#my-form-id');
      var method = form.attr('method').toLowerCase();      // "get" or "post"
      var action = form.attr('action');                    // url to submit to
      $[method](action, form.serialize(), function(data){
        // Do something with the server response data      
        // Or at least let the user know it saved
      });
    },10000);                                              // do it every 10 seconds
    

    If you don't want to use the method of the form, but always want to use 'post', then use:

    $.post(action, form.serialize(), ... );
    

    And, if you want to supply your own action for the autosave that is different from the action for the actual save:

    $.post("/autosave/comments", form.serialize(), ... );
    

提交回复
热议问题