2013-01-19 51 views
0

我在index.html中有一个文本框,我想获取它的值并将其放到另一个html文件result.html中的文本框中。 (他们使用相同的.js文件)获取文本框的值到另一个html文件

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?

在此先感谢。

+1

如果你有通过这个值横跨你可能想使用cookie来使所有网页的数据持久化尝试没有服务器端的帮助。 – Lix

+0

或研究html5功能。我认为这有点像本地存储。这可能是你正在寻找,可能也完全不符合... –

+0

@MAXIMUM请考虑绿色检查答案,如果答案服务你的目的。更多详细信息在这里如何做到这一点http://meta.stackexchange。 COM /问题/ 23138 /如何对接受-的回答上堆栈溢出 –

回答

1

使用的查询字符串筛选

http://mysite.com/index.html?name=john 

,并通过使用javascript

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

更改结果页面的网址如下得到不同的HTML页面上此名字;

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> 
相关问题