dongtuo2373 2014-11-14 17:37
浏览 31
已采纳

防止保存html内容的多个副本

I have been working on part of a program that saves the html of the currently displayed page to another file upon pressing button. My problem is that it works just fine if you pause in between button presses a few seconds but if you press the button more than once quickly it will save the file but the html in the file is not correct. It basically looks like it is saving multiple copies of the original html contents to the file.

Below is the code I am using:

HTML:

<form method="POST" action="app/savePage.php" id="file-options-form" name="file-options-form">

    <!--used to store content before it is saved-->
    <input type="hidden" value="" name="content" id="tm-content">

    <!--used to store scripts before it is saved-->
    <input type="hidden" value="" name="scripts" id="tm-scripts">

    <!--used to store styles before it is saved-->
    <input type="hidden" value="" name="styles" id="tm-styles">

    <span id="file-button-container">
        <input type="button" name="new-page-button" value="New Theme" id="new-page-button">
        <input type="submit" name="save-button" value="Save Theme" id="save-button">
        <label for="theme-name">Theme Name:</label>
        <input type="text" value="" name="theme-name" id="theme-name">
    </span>

</form>

Javascript/JQuery:

$("#file-options-form").submit(function(e){//begin function

 //stop form from submitting
 e.preventDefault(); 

 //the content to save
 var contentToSave = [];

 //add the html of the file to the contentToSave array
 contentToSave.push($("html")[0].outerHTML);

 //store the content in the #tm-content hidden input box
 $("#tm-content").val(contentToSave.join(""));

 //url for saveTheme.php file
 var url = "/thememaker/app/saveTheme.php";

 //begin ajax function
 $.ajax({//begin ajax function
    type:"POST",
    url: url,
    data:$(this).serialize(),
     success:function(data){//begin success function

     },//end success function
     error:function(e){

        alert(e);

     }

 //todo: fix multiple button press problem
 });//end ajax function

});//end function

Attempted fixes:

  1. I have tried disabling the button on click and then enabling it on the ajax success function

  2. I have tried hiding the button on click and then enabling on the ajax success function

  • 写回答

1条回答 默认 最新

  • dps43378 2014-11-15 00:25
    关注

    This was the real solution to my problem. I was not clearing the contents of the #tm-content input box after submitting the form successfully.

           $("#file-options-form").submit(function(e){//begin function
    
                 //stop form from submitting
                 e.preventDefault(); 
    
                //the content to save
                var contentToSave = [];
    
               //add the html of the file to the contentToSave array
               contentToSave.push($("html")[0].outerHTML);
    
              //store the content in the #tm-content hidden input box
              $("#tm-content").val(contentToSave.join(""));
    
              //url for saveTheme.php file
              var url = "/thememaker/app/saveTheme.php";
    
             //begin ajax function
             $.ajax({//begin ajax function
                    type:"POST",
                     url: url,
                     data:$(this).serialize(),
                     success:function(data){//begin success function
    
    
               //clear the content when it is submitted successfully
               $("#tm-content").val(""); //<------code that fixed the problem
    
             },//end success function
                error:function(e){
    
                 alert(e);
    
             }
    
    
             });//end ajax function
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog