Confirm to leave the page when editing a form with jQuery

前端 未结 5 2092
无人共我
无人共我 2020-12-05 00:50

I am coding a form and I need a function like stackoverflow have: \"You have started writing or editing a post.\".

I\'ve looked through the code of stackoverflow to

5条回答
  •  眼角桃花
    2020-12-05 01:30

    This is how in JQuery

    $('#form').data('serialize',$('#form').serialize());
      // On load save form current state
    
    $(window).bind('beforeunload', function(e){
        if($('#form').serialize()!=$('#form').data('serialize'))return true;
        else e=null;
        // i.e; if form state change show box not.
    });
    

    You can Google JQuery Form Serialize function, this will collect all form inputs and save it in array. I guess this explain is enough :)

提交回复
热议问题