简体   繁体   English

Javascript如何将多个文本框复制到一个文本框中

[英]Javascript how to copy multiple text boxes into one text box

I have three separate text boxes and want a button which copies all the content from the boxes into a 4th box. 我有三个单独的文本框,并希望有一个按钮将所有内容从框中复制到第4个框中。 How can I do this with javascript? 如何使用javascript执行此操作?

<form>
<textarea cols="60" rows="5" id="box1">PAST: </textarea>
<br /><br />
<textarea cols="60" rows="5" id="box2">PRESENT: </textarea>
<br /><br />
<textarea cols="60" rows="5" id="box3">FUTURE: </textarea>

<br /><br />
<input name="" type="button" />
<br /><br />
<textarea cols="60" rows="5" id="box4">All Past Present Future</textarea>
</form>

You just extract textarea values - and make their concatenation a value of this 'aggregate' textarea. 您只需提取textarea值-并将它们的串联作为此“汇总” textarea的值即可。 It's quite easy to do with jQuery, like this: 使用jQuery非常容易,如下所示:

$('#button_id').click(function() {
  $('#box4').val(
    $('#box1').val() + $('#box2').val() + $('#box3').val()
  );
});

try this: 尝试这个:

$('button').click(function(){
    var text = "";
    $('textarea:not(:eq(3))').each(function(){
       text += this.value
    })
    $('textarea:eq(3)').val(text)    
})

demo 演示

jsFiddle: http://jsfiddle.net/wCPbY/3/ jsFiddle: http : //jsfiddle.net/wCPbY/3/

// Runs on document ready
$(document).ready(function()
{
    // Grabs each text in the textareas based on the id, added spaces in between.
    var text = $("#box1").text() + " " + $("#box2").text() + " " + $("#box3").text();

    // Appends the text to box4.
    $("#box4").text( text );
});​

With jQuery you can create a selector that will capture all the required elements. 使用jQuery,您可以创建一个选择器,该选择器将捕获所有必需的元素。 You might want to alter your HTML to give the elements matching class attributes so the selector will be able to group them together. 您可能想要更改HTML,以提供与类属性匹配的元素,以便选择器能够将它们分组在一起。

<textarea cols="60" rows="5" class="textGroup" id="box1">PAST: </textarea>
<textarea cols="60" rows="5" class="textGroup" id="box2">PRESENT: </textarea>
<textarea cols="60" rows="5" class="textGroup" id="box3">FUTURE: </textarea>
<textarea cols="60" rows="5" id="box4">All Past Present Future</textarea>

The selector to get all of the textareas would be - $(".textGroup") . 获取所有文本区域的选择器将是- $(".textGroup")
Now all we have to do is iterate over all of them, collecting their contents and appending it to the main All Past Present Future textarea. 现在,我们要做的就是遍历所有这些对象,收集它们的内容并将其附加到主要的All Past Present Future文本区域中。

var wholeString = '';
$(".textGroup").each(function(index,elem){
  wholeString += $(elem).text();
});

$("#box4").text(wholeSrting);

You don't need to load in an entire JavaScript library like jQuery. 您无需像jQuery一样加载整个JavaScript库。 Try this simple JavaScript (built for scalability!) 试试这个简单的JavaScript(为可伸缩性而构建!)

var textboxes = document.getElementsByTagName('textarea'),
    box4 = document.getElementById('box4'),
    i;

for (i = 0; i < textboxes.length - 1; i++) {
    box4.value += textboxes[i].value;
}

And here is your jsFiddle . 这是您的jsFiddle

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

相关问题 数据绑定多个文本框以在一个文本框中显示 - data-bind multiple text boxes to display in one text box 填充一个文本框后如何动态添加文本框? - How to add text boxes dynamically when one text box is filled? 复选框的Javascript,用于将帐单文本框复制到运输文本框 - Javascript for checkbox to copy billing text boxes over to shipping text boxes 如果可以为空,如何使用JavaScript添加多个货币文本框? - How to add multiple text boxes of currency using JavaScript when one could be empty? 通过单击按钮填充文本框仅填充一个文本框 - Populating text boxes with click on buttons only fills one text box 如何使用javascript字符计数器添加多个文本框的总计 - How to have a javascript character counter add multiple text boxes' totals 如何使用javascript通过另一个文本框的输入值更改两个文本框的显示值? - How to change the display value of two text boxes through the input value of another text box using javascript? 用于chrome的html复制命令可复制多个文本框 - Copy command in html for chrome to copy multiple text boxes 从多个具有相同名称的文本框中获取带有javascript的输入框的值 - Get value of input box with javascript from multiple text boxes with same name 一个函数返回多个文本框的结果 - one function returns result for multiple text boxes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM