Javascript 使用“提交”按钮将值从一个文本框复制到另一个文本框

Javascript 使用“提交”按钮将值从一个文本框复制到另一个文本框,javascript,Javascript,我知道这可以通过Javascript实现,我正在学习,所以请告诉我,当我单击更新按钮时,我希望将文本框中的文本复制到另一个文本框中。假设您有: jQuery solution - check it out (jQuery that is) $('#button').click(function(e) { e.preventDefault(); $('#totextarea').val($('#fromtextarea').val()); ...then submit the form

我知道这可以通过Javascript实现,我正在学习,所以请告诉我,当我单击更新按钮时,我希望将文本框中的文本复制到另一个文本框中。

假设您有:

jQuery solution - check it out (jQuery that is)
$('#button').click(function(e) {
  e.preventDefault();
  $('#totextarea').val($('#fromtextarea').val());
  ...then submit the form if you wish to or whatever...
  $('#theform').submit();
});
<textarea id="source"></textarea>
...
<textarea id="target"></textarea>
...
<button type="button" onclick="update();">Update</button>

函数sync()
{
//按元素ID获取第一个和第二个值
var n1=document.getElementById('n1');
var n2=document.getElementById('n2');
//将第一个文本框的值指定给第二个文本框
n2.value=n1.value;
}
使同步
尝试以下操作

<script>
function onSubmitClick() {
  var box1 = document.getElementById('box1');
  var box2 = document.getElementById('box2'); 
  box2.value = box1.value;
}
</script>

<textarea id='box1'></textarea>
<textarea id='box2'></textarea>
<button onclick='onSubmitClick(); return false'>Click Me</button>

函数onSubmitClick(){
var box1=document.getElementById('box1');
var box2=document.getElementById('box2');
box2.value=box1.value;
}
点击我
JSFIDLE演示


您可以发布到目前为止的代码吗?请包括您的html和JS
<script>
function sync()
{
  // Take first and second value by element ID
  var n1 = document.getElementById('n1');
  var n2 = document.getElementById('n2');
  // Assign the value of the 1st to the 2nd text box
  n2.value = n1.value;
}
</script>

<input type="text" name="n1" id="n1" />
<input type="text" name="n2" id="n2"/>
<!-- you put a function sync to be executed on click on the button --> 
<button onclick="sync()">Synchronize</button>
<script>
function onSubmitClick() {
  var box1 = document.getElementById('box1');
  var box2 = document.getElementById('box2'); 
  box2.value = box1.value;
}
</script>

<textarea id='box1'></textarea>
<textarea id='box2'></textarea>
<button onclick='onSubmitClick(); return false'>Click Me</button>