表格保留以前的数据

时间:2013-12-20 05:50:44

标签: javascript jquery html

<script type="text/javascript">


$(document).ready(function(){

var count=$('#option1').val(); 
var counter = 1;

$("#addButton").click(function () {

if(counter>count){
        alert("Only "+count+" textboxes allow");
        return false;
}   

var newTextBoxDiv = $(document.createElement('div'))
     .attr("id", 'TextBoxDiv' + counter);

newTextBoxDiv.after().html('<input type="radio" name="answer" id="answer" value="' + counter + '" /><label>Option #'+ counter + ' : </label>' +
      '<textarea name="textbox' + counter + 
      '" id="textbox' + counter + '" value="" />');

newTextBoxDiv.appendTo("#TextBoxesGroup");

tinymce.init({
        selector: "textarea",
        force_p_newlines : false,


    plugins: [
            "advlist autolink autosave image link  lists charmap print preview hr anchor pagebreak spellchecker",
            "searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
            "table contextmenu directionality emoticons template textcolor paste fullpage textcolor"
    ],

    toolbar1: "newdocument fullpage | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | styleselect formatselect fontselect fontsizeselect | cut copy paste | searchreplace | bullist numlist | outdent indent blockquote | undo redo | link unlink anchor image media | inserttime preview ",
    toolbar2: "forecolor backcolor | table | hr removeformat | subscript superscript | charmap emoticons | print fullscreen | ltr rtl | spellchecker | visualchars visualblocks nonbreaking template pagebreak restoredraft ",

    image_advtab: true,
    menubar: false,
    toolbar_items_size: 'small',

    style_formats: [
            {title: 'Bold text', inline: 'b'},
            {title: 'Red text', inline: 'span', styles: {color: '#ff0000'}},
            {title: 'Red header', block: 'h1', styles: {color: '#ff0000'}},
            {title: 'Example 1', inline: 'span', classes: 'example1'},
            {title: 'Example 2', inline: 'span', classes: 'example2'},
            {title: 'Table styles'},
            {title: 'Table row 1', selector: 'tr', classes: 'tablerow1'}
        ],

        templates: [
                {title: 'Test template 1', content: 'Test 1'},
                {title: 'Test template 2', content: 'Test 2'}
        ]

});
//add tinymce to this
tinyMCE.execCommand("mceAddControl", false, 'txt'+counter);

    counter++;
});

</script>




<form method="post" name="myForm" id="form" action="upload"  onsubmit="return validateForm()" >
<div id='TextBoxesGroup'>
    <div id="TextBoxDiv0">
        <label>Selection: </label><select name="sel" id="sel" >
          <option selected="true" style="display:none;">Select </option>
 <%  
  ResultSet rsta1=st.executeQuery("SELECT DISTINCT term FROM termwhere term not like 'null'"); 
  while(rsta1.next()) {
      out.write("<option value=" + rsta1.getString("term") + ">" + rsta1.getString("term") + "</option>");
  }
%> 
        <label>Question: </label><textarea id='textbox0' name="textbox0"></textarea>
        <input type="hidden" name="option1" id="option1" value="<%=options21 %>"/>
    </div>
</div>
<input type='button' value='Add Button' id='addButton' />
<input type='button' value='Remove Button' id='removeButton'/>
<input type='submit' value='Submit'/>
 </form>

我有一个Jquery,当我按下添加按钮时,会出现更多的文本区域。这个工作正常,这个页面的提交也有效。下次我回来时会出现问题。

之前输入textareas的所有数据再次出现,相同的旧数据填充在textareas中。 我应该怎么做才能使表单字段为空?

我得到的另一个问题是,当我添加新文本字段时,数据会刷新到旧数据。例如。

当我在一个textarea中添加文本并按下添加按钮时,我输入的文本消失或者上一个文本出现(我的第一个问题,如上所述)。同样,当我再次按下添加按钮时,前两个文本字段被清空或者出现上一个文本。 为什么会这样?

1 个答案:

答案 0 :(得分:0)

尝试在表单上添加autocomplete = off,以防止浏览器保留信息

参见 - &gt; https://developer.mozilla.org/en-US/docs/Mozilla/How_to_Turn_Off_Form_Autocompletion

OR

使用javascript清空表单

formObject.reset()