JavaScript -保留"textbox2"值,以便在刷新/重新打开页面后可用
JavaScript - retain "textbox2" value for it to be available after refresh/reopen page
我有两个文本框。在第一个中写入内容并单击OK按钮后,值也出现在textbox2中。我需要在页面刷新后保存该值,而不是修改,直到有人在textbox1中引入新值并再次点击OK。
<html>
<head> </head>
<script type="text/javascript">
function myfunction()
{
var first = document.getElementById("textbox1").value;
var textbox2 = document.getElementById("textbox2");
textbox2.value = first;
}
</script>
<body>
<input type="text" name="textbox1" id="textbox1" />
<input type="submit" name="button" id="button1" onclick="myfunction()" value="OK" />
<br/>
Your answer is:
<input type="text" name="textbox2" id="textbox2" readonly="true"/>
</body>
</html>
据我所知,您可以通过声明runat="server"使"Textbox2"成为服务器控件,即使在服务器击中后也会保留该值,如下所示。
<input type="text" name="textbox2" id="textbox2" readonly="true" runat="server" />
然后将其存储在会话(在c#的情况下)或服务器端类似的东西中以恢复值。
希望这有帮助!!
每次都应该这样做。http://jsfiddle.net/CKLAg/
首先,简化你的HTML:
<input type="text" name="textbox1" id="textbox1">
<button name="button" id="button1" onclick="myFunction();">OK</button>
<br/>Your answer is:
<input type="text" name="textbox2" id="textbox2" readonly="true">
然后,添加以下函数来检查localStorage, save和load storage的需求:注意,您必须使用jQuery来获取文档就绪状态,并在DOM就绪时设置输入元素。
$('document').ready(function(){
var prevAnswer = loadStorage;
$('#textbox2').attr('value', prevAnswer);
});
function loadStorage() {
if (supports_html5_storage) {
if (localStorage.getItem('myAnswer')) {
var answer = localStorage.getItem('myAnswer');
return answer;
}
}
}
function myFunction() {
var first = document.getElementById("textbox1").value;
var textbox2 = document.getElementById("textbox2");
textbox2.value = first;
if (supports_html5_storage) {
alert('storing');
localStorage['myAnswer'] = first;
}
}
function supports_html5_storage() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
相关文章:
- Jquery Onclick将数据发送到新打开的窗口
- 如何重定向页面并在新打开的页面上突出显示某些文本
- JavaScript禁用”;打开”"打开新标签”;以及“;复制“;长按iOS 8 safari中的锚标签
- 新打开的选项卡会终止函数调用
- 将脚本执行保留在新打开的页面中
- 是否可以将新打开的窗口移动到另一个显示器
- 如何在新打开的窗口中设置文本框值
- Safari 8 中新打开的对话框的高度不正确
- 如何为重定向或其他服务器上新打开的页面设置超时
- Javascript:在新打开的窗口中找不到函数
- 如何从原始选项卡中获取新打开的浏览器选项卡名称
- 点开的窗口隐藏了新打开的窗口的一半
- 如何通过javaScript获取新打开选项卡的URL
- JavaScript异常”;调用的对象已与其客户端断开连接"打开(..)
- Zombie.js/Pantom.js可以通过window.open获取新打开的窗口的HTML吗
- 在新打开的页面上使用ScrollTo()
- JQuery-在一个新打开的窗口中加载多个页面
- "打开“;切换按钮jQuery
- 如何在新打开的窗口中调试JS
- 如何为新打开的窗口设置ID