简体   繁体   English

Javascript变量等于文本框值

[英]Javascript variable equal a textbox value

I am getting a problem when I try to pass a the pop_size variable equal to the value of the textbox. 我尝试传递等于文本框值的pop_size变量时遇到问题。 This variable will be used in along the script with the value of textbox. 该变量将与文本框的值一起在脚本中使用。 When I click on the button the value change and update the value of variable and then I reload the page and the variable is set to the value in the textbox. 当我单击按钮时,值会更改并更新变量的值,然后重新加载页面,变量将设置为文本框中的值。

<form name="myform1" action="" method="get"> 
    Input the number of populations<br />
    <input type="number" name="pop" id="pop" value=5 /><br />
    <input type="button" name="b1" id="b1" value="Click to set" 
           onClick="setValue()" /><br /> 
</form>  

function setValue() { 
    var test, test1;
    test=parseInt(document.getElementById('pop').value);
    pop_size = test;
} 

If I'm understanding you properly, you're having a variable set that works fine until you reload, but then gets lost. 如果我对您的理解正确,那么您将拥有一个变量集,该变量集在重新加载之前可以正常工作,但随后会丢失。

There are two basic strategies I know of for making a variable survive a page reload. 我知道有两种基本策略可以使变量在页面重新加载后仍然有效。

The first one I'll list seems more complicated. 我要列出的第一个似乎更复杂。 I'll give you the basics of it, and can tell you more if you need. 我将为您提供基础知识,并在需要时告诉您更多信息。 Once you have the value, append it to the address of the web page within the query string. 获得值后,将其附加到查询字符串内的网页地址。 It would be something like window.location += "?pop_size=" + pop_size; 就像window.location += "?pop_size=" + pop_size; but that has a lot of inherent difficulties: you'll have to have a script that checks for the query string and extracts pop_size from it, and you should replace any existing pop_size in the query string when you update it. 但这有很多固有的困难:您必须拥有一个脚本来检查查询字符串并pop_size提取pop_size ,并且在更新查询字符串时应替换查询字符串中任何现有的pop_size。 It can work, and you can find plenty of web pages that discuss query strings and javascript, but there are easier ways. 它可以工作,并且您可以找到很多讨论查询字符串和javascript的网页,但是有更简单的方法。

If you're using HTML5, the much easier solution is to use sessionStorage . 如果您使用的是HTML5, 则更简单的解决方案是使用sessionStorage It's currently supported in all major browsers , but the first link I provided (to MDN) gives you a polyfill that can give you backward compatibility. 目前所有主要浏览器都支持该功能 ,但是我提供的第一个链接(至MDN)为您提供了一个polyfill,可以向后兼容。 It will let you store variables on the browser that will stay live until the user closes the browser window. 它可以让您在浏览器中存储变量,这些变量将一直存在,直到用户关闭浏览器窗口为止。

To save the value, MDN recommends that you do: 要保存该值,MDN建议您执行以下操作:

sessionStorage.setItem("pop_size", pop_size);

and retrieve the values on pageLoad with: 并使用以下方法检索pageLoad上的值:

document.getElementById('pop').value = sessionStorage.getItem("pop_size") || 5;  // if it's not found, default back to 5.

Honestly, I've never seen that syntax, and I'm not sure why MDN recommends it. 老实说,我从未见过这种语法,而且我不确定为什么MDN推荐它。 The more standard usage of the sessionStorage object is even easier: sessionStorage对象的更标准用法更加容易:

sessionStorage.pop_size = pop_size;
...
document.getElementById('pop').value = sessionStorage.pop_size || 5; // if it's not found, default back to 5.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM