简体   繁体   English

JavaScript 输入字段在加载时获取旧值

[英]JavaScript Input field gets the old value on loading

I have an input field:我有一个输入字段:

<input id="RangeRate1" type="range" min="125.00" max="500.00" value="250.00">

When I reload the page, the value of the input is set to what was set the last time the range slider was changed.当我重新加载页面时,输入值设置为上次更改范围 slider 时设置的值。 For instance, if the rate was changed to 300 prior to page reload, when the page reloads, if I console out the value of the range slider:例如,如果在页面重新加载之前将速率更改为 300,当页面重新加载时,如果我控制台输出范围 slider 的值:

let RateSlider = document.querySelector("RangeRate1")
console.log("existing rate: ", RateSlider.value)

I get 300. even though the html input value is still shown as 250.00 as above.我得到 300。即使 html 输入值仍显示为 250.00,如上所示。 When I use ctrl shift r , the correct number is displayed, in this case the value I see in the input HTML code, 250. So, is there a way to flush out the old value or overwrite it with the actual value in the input tag lin?当我使用ctrl shift r时,会显示正确的数字,在这种情况下,我在输入 HTML 代码中看到的值是 250。那么,有没有办法清除旧值或用输入中的实际值覆盖它标签林?

Some browsers do this for "convenience" purposes even though it's less than convinient一些浏览器出于“方便”的目的这样做,即使它不太方便

You can just use some JavaScript to set the actual value to the attribute value (unless the attribute itself is also set by the browser to last value, in which case just set it to the hard coded value itself)您可以只使用一些 JavaScript 将实际值设置为属性值(除非浏览器也将属性本身设置为最后一个值,在这种情况下只需将其设置为硬编码值本身)


let RateSlider = document.querySelector("#RangeRate1")

RateSlider.value=parseInt(RateSlider.getAttribute("value"))

EDIT alternatively if the input is part of a form (assuming form has id if "myForm") you can reset it或者,如果输入是表单的一部分(假设表单有 id 如果是“myForm”),则可以进行编辑,您可以重置它

myForm.reset()

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

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