如何清除 history.back() 上的输入文件和输入文本值

How to clear input file and input text values on history.back()

有一个非常相似的问题 但我正在做一些不同的事情。在我提交表单的情况下,如果出现问题,我正在执行 history.back() 以将所有值保留在表单中,但在输入文件的情况下,文件名保留在那里但实际上没有文件那里。因此,为了避免混淆并让用户知道它需要再次上传文件,我只想清除该元素的值,以及另一个具有文件描述的输入文本。

向其添加 autocomplete="off" 属性可以防止浏览器缓存该值,但这适用于 Chrome 但不适用于 IE11.

我也可以使用 JQuery 来做到这一点,比如 $input_file.val('').end();,但是因为我在 history.back() 之后尝试这样做,我不确定为什么。

<script>
      alert("Error");
      window.history.back();
      $('input[id="Attachment"]').val('').end();
</script>

欢迎提出有关如何实现此目的的任何其他想法。

有一个名为 pageShow 的触发器,它会在过渡动画完成时触发。

jQuery( ".selector" ).on( "pageshow", function( event ) { ... } )

对于您的情况,您需要执行以下操作

<script>
  alert("Error");
  window.history.back();
  $(window).bind("pageshow", function() {
      //debugger; you can check and see if this block of code gets triggered 
      $('input[id="Attachment"]').val('').end();
   });
</script>

Note: The triggering of this event is deprecated as of jQuery Mobile 1.4.0. It will no longer be triggered in 1.6.0. The replacement for pageshow is the pagecontainer widget's pagecontainershow event. In jQuery Mobile 1.4.0, the two events are identical except for their name and the fact that pagecontainershow is triggered on the pagecontainer, whereas pageshow is triggered on the page.

jQuery Reference to the event

examples