我在index.html中有一个文本框,我想获取它的值并将其放在另一个html文件(即result.html)的文本框中。 (他们使用相同的.js文件)
index.html文本框:
<input id="txt2" readonly="true" type="number" value="0" name="score" style="border:none;background-color:#f0f2f7">
result.html文本框:
<input id="txt3" readonly="true" type="number" value="0" name="score" style="border:none;background-color:#f0f2f7"></td>
这是代码,它会自动转到另一页:
if((mins == 0) && (secs == 0)) {
//window.alert("Time is up.Your score is "+score); // change timeout message as required
document.getElementById('txt2').value = score;
window.location = "score.html" // redirects to specified page once timer ends and ok button is pressed
}
else {
cd = setTimeout("redo()",1000);
}
txt3将如何获得txt2的值,因为它们位于不同的html中?
提前致谢。
答案 0 :(得分:1)
使用查询字符串像这样
http://mysite.com/index.html?name=john
并使用javascript在不同的html页面上获取此名称
function loadPageVar (sVar) {
return unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(sVar).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
}
// Would alert the value of QueryString-variable called name
alert(loadPageVar("name"));
答案 1 :(得分:1)
更改结果页面URL如下;
window.location = "result.html?val="+document.getElementById('txt2').value;
result.html
<input id="txt3" readonly="true" type="number" value="0" name="score" style="border:none;background-color:#f0f2f7"></td>
<script>
document.getElementById('txt3').value=window.location.search.replace("?val=","");
</script>