获取文本框的值到另一个html文件

Getting the value of textbox to another html file

本文关键字:另一个 html 文件 取文本 获取      更新时间:2023-09-26

我在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?

像这样使用查询字符串

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")); 

更改结果页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>