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

时间:2011-04-02 18:47:10

标签: javascript

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

4 个答案:

答案 0 :(得分:1)

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();
});

答案 1 :(得分:1)

假设你有这个:

<textarea id="source"></textarea>
...
<textarea id="target"></textarea>
...
<button type="button" onclick="update();">Update</button>

然后你的JS函数可以是:

function update() {
    document.getElementById('target').value = document.getElementById('source').value;
}

答案 2 :(得分:0)

<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>

答案 3 :(得分:0)

尝试以下

<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>

JSFiddle演示