本文介绍了来自一个textarea的文本应该被复制到另一个textarea和原始textarea应该清除在按钮点击使用javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了下面的代码,它显示两个textarea,其中一个textarea的文本被复制到另一个textarea按钮点击使用javascript



 < head>< script type =text / javascript> function displayOut(){var input = document.getElementById(txt)。value; var text2 = document.getElementById(txt1); text2。 value = input; if(input.length === 0){alert(Please enter a valid input); return;} function eraseText(){document.getElementById(txt)。value =;}}< / script>   < body>< h1 id =result> Javascript Exm< / h1>< textarea id =txt1rows = cols =100readonly =readonly>< / textarea>< textarea id =txtrows =4cols =50onclick =eraseText()>< / textarea> ; input type =buttononclick =displayOut()value =click>< / body>  

p>

但是我需要 -



1)在按钮上点击文本应该被复制到另一个textarea和文本从原始文本。第一个textarea应该明确接受另一个文本,所以我hv使用擦除功能bt它不工作,第二是



2),我想显示

解决方案

尝试这个方法,然后在第二个文本区域中以一个连续的格式将文本复制到下一个按钮上。你没有犯错,你的 eraseText()移出 displayOut()。并在第二个文本区域中复制文本后调用 eraseText()



  function displayOut ){var input = document.getElementById(txt)。value; var text2 = document.getElementById(txt1); text2.value = input; if(input.length === 0){alert(Please enter a valid input);返回; } eraseText(); // call function to erase text in textarea。} function eraseText(){document.getElementById(txt)。value =;}  
  < textarea id =txt1rows =10cols = 100readonly =readonly>< / textarea>< textarea id =txtrows =4cols =50onclick =eraseText()>< / textarea>< input type =buttononclick =displayOut()value =click>  


i have done with following code it displays two textarea where the text from one textarea gets copied to another textarea on a button click using javascript

<head>
<script type="text/javascript">

function displayOut(){
var input=document.getElementById("txt").value;
var text2=document.getElementById("txt1");
text2.value=input;
if(input.length===0)
{
	alert("Please enter a valid input");
	return;
}
function eraseText() {
    document.getElementById("txt").value = "";
}
}
 

</script>
<body>
<h1 id="result">Javascript Exm</h1>



<textarea id="txt1" rows="10" cols="100" readonly="readonly" ></textarea>
<textarea id="txt" rows="4" cols="50"  onclick="eraseText()"></textarea><input type="button" onclick="displayOut()" value="click">


</body>

but i need that -

1)on a button click the text should get copied to another textarea and the text from origial textare ie. first textarea should get clear to accept the another text, so i hv use erase function bt it doesn't work and second is that

2) and i want to display that the text should gets copied in second textarea in a continuous format one below the other on a button click.

解决方案

Try this one, you did little mistake , your eraseText() move out from displayOut(). and call eraseText()function after copied text in second textarea.

function displayOut() {
  var input = document.getElementById("txt").value;
  var text2 = document.getElementById("txt1");
  text2.value = input;
  if (input.length === 0) {
    alert("Please enter a valid input");
    return;
  }
  eraseText(); //call  function to erase text in textarea.

}

function eraseText() {
  document.getElementById("txt").value = "";
}
<h1 id="result">Javascript Exm</h1>

<textarea id="txt1" rows="10" cols="100" readonly="readonly"></textarea>
<textarea id="txt" rows="4" cols="50" onclick="eraseText()"></textarea>
<input type="button" onclick="displayOut()" value="click">

这篇关于来自一个textarea的文本应该被复制到另一个textarea和原始textarea应该清除在按钮点击使用javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:19