admin管理员组文章数量:1337212
Trying to find an elegant solution to reset a webform (or more specifically two INPUT Type='hidden'
fields) if a user goes BACK after the form is submitted.
I understand that BACK button does not trigger an onload event.
But is there any way of dealing with this?
Trying to find an elegant solution to reset a webform (or more specifically two INPUT Type='hidden'
fields) if a user goes BACK after the form is submitted.
I understand that BACK button does not trigger an onload event.
But is there any way of dealing with this?
Share Improve this question asked Dec 15, 2011 at 10:34 Uno Mein AmeUno Mein Ame 1,0906 gold badges16 silver badges30 bronze badges 3- You can use cookies with javascript to mark that the user have been on this page before, or whether he have filled the form before – Teneff Commented Dec 15, 2011 at 10:41
- can you please suggest the code, i'm not really familiar with cookies – Uno Mein Ame Commented Dec 15, 2011 at 10:45
- People usually do not want this. It's extremely annoying especially when ing back from an error page. – ThiefMaster Commented Dec 15, 2011 at 10:52
3 Answers
Reset to default 7when the back button is pressed, onbeforeunload
event is fired so you can try something like
jQuery:
$('#form_id').submit(function(){
$(window).unload(function () {
$('input[type="hidden"]').val("reset_value");
}
});
javascript:
<form onsubmit="call_function();">
in between script tag:
function call_function(){
window.onbeforeunload = function(){
var input_elems = document.getElementByTagName('input');
for(var i=0;i<input_elems.length;i++){
if(input_elems[i].type === 'hidden'){
input_elems[i].innerHTML = "reset_value";
}
}
}
You could prevent the default submit from fireing, ajax-post the form, then unset the unwanted fields and change the location to a new page. Isnt really nice, but should do the trick.
For IE 8 or less you can reset the form :
document.getElementById('formname').reset();
For IE 9, Firefox, Chrome you can reset just the fields you want by using:
elementNodeHere.value = elementNodeHere.getAttribute('value');
Don't bother with jquery,
.attr('value')
returns the new value not the original
(getAttribute
in IE 7 returns the new value only and in IE 8 it can return NULL
)
本文标签: javascriptReset form on Back buttonStack Overflow
版权声明:本文标题:javascript - Reset form on Back button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743384013a2493735.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论