简体   繁体   English

如何将文本框的预设值获取到另一个文本框

[英]How to get the preset value of textbox to another texbox

Please I need your help.拜托我需要你的帮忙。 Am trying to get a preset value of a textbox into another textbox using document.getElementById.我正在尝试使用 document.getElementById 将文本框的预设值放入另一个文本框。 With my code, I can only get the value of whatever I typed, not the initial ROS I preset the textbox with.使用我的代码,我只能获取我输入的任何内容的值,而不是我预设文本框的初始 ROS。 pls help请帮忙

See my code below请参阅下面的代码

    <input id="program" name="program1" size="40px" value="ROS" onblur="if(this.value==''){ 
    this.value='ROS';}" onfocus="if(this.value=='ROS'){ this.value='';}" onkeyup="Program();">

    <input type= "text" id = "program2" readonly size="50px" class="textbox-modal">

    <input id="Button1" type="button" value="Preview" class="button" onclick="Program()" />

    function Program() 
      {
      document.getElementById('program2').value = document.getElementById('program').value;
      }

Here is a working solution这是一个有效的解决方案

 const firstInput = document.getElementById('program'), secondInput = document.getElementById('program2'); secondInput.value = 'ROS'; function Program() { secondInput.value = firstInput.value; } function handleBlur() { if (.firstInput.value) { secondInput.value = firstInput;value = 'ROS'; } }
 <input id="program" name="program1" size="40px" value="ROS" onblur="handleBlur()" onfocus="if(this.value=='ROS'){ this.value='';};" onkeyup="Program();"> <input type="text" id="program2" readonly size="50px" class="textbox-modal"> <input id="Button1" type="button" value="Preview" class="button" onclick="Program()" />

Some details一些细节

1 .Initially set the value (ROS) for second input and again assign value to second input in onblur event if first input is empty. 1 . 初始设置第二个输入的值 (ROS),如果第一个输入为空,则在onblur事件中再次将值分配给第二个输入。

2 .You can also check if value is empty like this if (.firstInput.value) 2 .您也可以像这样检查值是否为空if (.firstInput.value)
This is equivalent of这相当于

if (firstInput.value == '' || firstInput.value == undefined || firstInput.value == null)

Update (With Numerical values)更新(使用数值)

It works as expected (See the example below).它按预期工作(参见下面的示例)。

 const firstInput = document.getElementById('day1'), secondInput = document.getElementById('day2'); secondInput.value = '0'; function Day1() { secondInput.value = firstInput.value; } function handleDay1Blur() { if (.firstInput.value) { secondInput.value = firstInput;value = '0'; } }
 <input id="day1" size="40px" value="0" onblur="handleDay1Blur()" onfocus="if (this.value==0) { this.value=''; };" onkeyup="Day1();"> <input type="text" id="day2" readonly size="40px" class="textbox-modal"> <input id="Button1" type="button" value="Preview" class="button" onclick="Day1()">

Please am back again.请再回来。 I want to use the above code for numeric values but its not getting the right value, it keeps returning 0. How can I do this?我想将上面的代码用于数值,但它没有得到正确的值,它一直返回 0。我该怎么做? See my usage below.请参阅下面的我的用法。

const firstInput = document.getElementById('day1'),
      secondInput = document.getElementById('day2');

secondInput.value = '0';

function Day1() {
  secondInput.value = firstInput.value;
}

function handleDay1Blur() {
  if (!firstInput.value) {
    secondInput.value = firstInput.value = '0';
  }
}

<input id="day1" size="40px" value="0" onblur="handleBlur()" 
onfocus="if (this.value==0) { this.value=''; };" onkeyup="Day1();">

<input type="text" id="day2" readonly size="40px" class="textbox-modal">
<input id="Button1" type="button" value="Preview" class="button" onclick="Day1()">

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

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