永久修复HTML文本框值

时间:2018-03-08 18:03:55

标签: javascript html

enter image description here我正在研究GPS项目,我正在尝试在textbox1中获取高度(海平面)值并尝试将其转换为textbox2中的地平面值。

此处GPS海拔高度(ASL)值是动态的,例如..

Altitude reading (ASL) =  250.00 Mtr (initial value) textbox1
Altitude reading (ASL) =  300.00 Mtr (value increases to 50) textbox1
Altitude reading (ASL) =  400.00 Mtr (current value increases to 100) textbox1

Altitude reading (AGL) =  ASL(current value) - ASL(initial value)
Altitude reading (AGL) = 150 Mtrs. textbox2

正如我所说高度阅读(ASL)是动态的,如果我使用第三个文本框,例如textbox3来存储初始值,那么我如何锁定该值,以便在从文本框1获取后不应该更改。任何HTML的可能性还是java脚本?

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

解决方案是复制input的值,然后从文档中删除input并将值放入一个取代input的不可编辑元素中



var input1 = document.getElementById("txt1");
var input2 = document.getElementById("txt2");
var output = document.getElementById("output")
var container = document.getElementById("container");

input1.addEventListener("change", disable);
input2.addEventListener("input", sum);

function disable(evt){
  var val = input1.value;      // Get the value of the input
  container.innerHTML = "";    // Remove input from container
  container.textContent = val; // Place value in non-editible container
}

function sum(){
  output.textContent = +input1.value + +input2.value;
};

sum();

#output {
  display:inline-block;
  width:5em;
  padding:2px;
  background-color:#e0e0e0;
  border:1px solid #222;
}

Value 1: <span id="container"><input id="txt1"></span><br>
Value 2: <input id="txt2">
Sum: <span id="output"></span>

<div>(Change the value in the first input and then leave that field.)</div>
&#13;
&#13;
&#13;