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 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛