簡體   English   中英

提交后如何清除表單域的pre-post值,並用空白域重新填寫表單

[英]How to clear the pre-post values of form fields after submit, and fill the form again with the blank fields

這是我的網站http://exmaple.com/schedule-appointment/ ,我在那里應用了一個腳本來在提交后清除表單字段值。 基本上是在您第一次預約並完成時。 您再次返回同一頁面進行另一次約會,然后您將看到表格的所有字段都已填寫。 所以我不希望這種情況發生在實時網站上。 我使用了一個腳本來清除字段,但它在 firebug 控制台上工作,但在實時網站上沒有工作。 我之前做的有什么問題嗎,下面是我使用並提交給服務器的腳本。

<script type="text/javascript">
jQuery(document).ready(function (){
    jQuery('.form-horizontal').each(function(){ 
        alert("hello");
        this.reset(); 
    }); 
});
</script>

請幫助我們如何第二次清除表單字段以進行預約。

非常感謝!

可能是由瀏覽器自動完成引起的? 嘗試將autocomplete="off"添加到您的form標簽中。

https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion

<!DOCTYPE html>
<html>
  <head>
    <title>your page</title>
  </head>
  <body>
    <form action="">
      <div>
        <input type="text" id="sngl_txt">
      </div>
      <div>
        <textarea id="mlt_txt"></textarea>
      </div>
      <div>
        <select id="selection">
          <option value="">no selection (default)</option>
          <option value="1">option 1</option>
          <!-- and so on -->
        </select>
      </div>

      <div>
        <button type="button" id="rst_btn">reset form</button>
      </div>
    </form>
  </body>

<script type="text/javascript" src="link.to.your.jquery.library"></script>
<script type="text/javascript">

$(document).on('ready', function(){

  // prepare with sample values
  $('form').find('#sngl_txt').val('sample text');
  $('form').find('#mlt_txt').val('Lorem ipsum ....");
  $('form').find('#selection').val(1);

  var reset = function(){
    $('form').find('input, textarea, select').val('');
  }


  $('form').find('#rst_btn').on('click', reset);

  // or
  // $('form').find('#rst_btn').on('click', function(){
  //   reset();
  // });
  // if you don't want to use the reset funtion as listener

});

</script>
</html>

我知道在這個示例中一個簡單的按鈕 type="reset 也可以工作",但我經常需要使用自定義控件,我不得不手動將值設置為空字符串。

您可以嘗試的是$('form').reset()而不是$('.form-horizontal').reset() ,效果將與重置相同

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM