admin管理员组文章数量:1410717
I have a search text box after click in submit button textbox value is retain, but when i reload the page the textbox value still in a text box, how do i remove the value of text box after reloading/refreshing the page.
<script>
$(document).ready(function(){
document.getElementById("text1").value = localStorage.getItem("item1");
});
$(window).on('beforeunload', function() {
localStorage.setItem("item1",document.getElementById("text1").value);
});
</script>
Html.
<td>
<form action="/searchemp" method="post" >
Selected Employee <input type="text" name="EmployeeName" id="text1">
<input type ="submit" value="check">
</form>
Give me a help or suggestion to fix it.
Thank you.
I have a search text box after click in submit button textbox value is retain, but when i reload the page the textbox value still in a text box, how do i remove the value of text box after reloading/refreshing the page.
<script>
$(document).ready(function(){
document.getElementById("text1").value = localStorage.getItem("item1");
});
$(window).on('beforeunload', function() {
localStorage.setItem("item1",document.getElementById("text1").value);
});
</script>
Html.
<td>
<form action="/searchemp" method="post" >
Selected Employee <input type="text" name="EmployeeName" id="text1">
<input type ="submit" value="check">
</form>
Give me a help or suggestion to fix it.
Thank you.
Share Improve this question asked Apr 18, 2016 at 9:19 user6197865user6197865 12-
Why you can not delete the
value
from thelocalStorage
? – Kalpesh Rajai Commented Apr 18, 2016 at 9:26 - @ Kalpesh Rajai, how could i delete...?? – user6197865 Commented Apr 18, 2016 at 10:19
-
localStorage.clear();
@Rehan – Kalpesh Rajai Commented Apr 18, 2016 at 10:23 - You are submitting the form to another page or same page ? – Kalpesh Rajai Commented Apr 18, 2016 at 10:24
- If you Post the form to same page, So it is impossible to achieve that you want. Because when you are posting the page than it post the data to server and reload the page and in same way while reloading/ refreshing the page is reloaded.. it work same. – Kalpesh Rajai Commented Apr 18, 2016 at 10:29
3 Answers
Reset to default 1 $(document).ready(function () {
$("#form1").submit(function () {
window.localStorage['text1_val'] = $("input[name = 'text1']").val();
});
$(window).load(function () {
$("input[name = 'text1']").val(window.localStorage['text1_val']);
window.localStorage['text1_val'] = '';
});
});
<form id="form1" method="post">
<input type="text" name="text1">
<input type="submit">
</form>
By default a textbox value is empty at page loading. I guess your problem is due to storing and fetching the value from the local storage. The code responsible for the text appearance
$(document).ready(function(){
document.getElementById("text1").value = localStorage.getItem("item1");
});
Why don't you clear the localstorage?
$(window).unload(function() {
window.localStorage.removeItem(item1);
});
Code is not tested, but this should give you an idea...
本文标签: javascriptHow to retain textbox value after click submit buttonStack Overflow
版权声明:本文标题:javascript - How to retain textbox value after click submit button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744788025a2625118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论