简体   繁体   English

如何在表单中找到所有输入字段并使用Javascript将其值复制到textarea?

[英]How do I find all input fields inside a form and copy their values into textarea using Javascript?

I want to add a couple of input fields ( name , age , location , status , etc...) to my form. 我想在表单中添加几个输入字段( 名称年龄位置状态等)。

When I click submit button, how do I get all the values inside a form and place them into a single textarea element, so that it's easy to copy? 当我单击“ submit按钮时,如何获取表单中的所有值并将它们放入单个textarea元素中,以便于复制? Thanks for the help! 谢谢您的帮助!

My current code: 我目前的代码:

<form>
    <input type="text" id="txt">
    <button type="button" onclick="ShowText();">Submit</button>
</form>
<span id="show"></span>
<script>
    function ShowText(){
        var txt = document.getElementById('txt');
        var show = document.getElementById('show');
        show.innerHTML = txt.value;
    }
</script>

Don't use id attributes of input fields in that particular case. 在特定情况下,请勿使用输入字段的id属性。

Add an id attribute to the form form添加id属性

 function ShowText(){ // find each input field inside the 'myForm' form: var inputs = myForm.getElementsByTagName('input'); // declare 'box' variable (textarea element): var box = document.getElementById('show'); // clear the 'box': box.value = ''; // loop through the input elements: for(var i=0; i<inputs.length; i++){ // append 'name' and 'value' to the 'box': box.value += inputs[i].name + ': '+inputs[i].value+'\\n'; } } 
 <form id="myForm"> <input type="text" name="name" placeholder="Name"/><br/> <input type="text" name="age" placeholder="Age"/><br/> <input type="text" name="location" placeholder="Location"/><br/> <input type="text" name="status" placeholder="Status"/><br/> <button type="button" onclick="ShowText();">Submit</button> </form> <p>Show:</p> <p><textarea cols=30 rows=5 id="show"></textarea></p> 

You can display the text data in a paragraph and then can select it easily as: 您可以在段落中显示文本数据,然后可以轻松地选择它:

<!DOCTYPE html>
<html>
<body> 

<h1>My Web Page</h1>

<p id="demo">A Paragraph</p>
<form>
    <input type="text" name="in01" id="txt1">
    <input type="text" name="in02" id="txt2">
    <button type="button" onclick="ShowText();">Submit</button>
</form>

<span id="show"></span>
<script>
    function ShowText(){
        var txt = document.getElementById('txt1').value;
        var show = document.getElementById('txt1').value;
        //window.alert(txt);
         document.getElementById("demo").innerHTML = txt +" "+show;
    }
</script>
</body>
</html>

暂无
暂无

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

相关问题 如何使用JavaScript向输入字段添加值? - How do I add values to input fields using JavaScript? 如何使用jquery和/或javascript在textarea内查找链接标记并将其转换为@import标记? - how do i find and convert link tags to @import tags inside a textarea using jquery and/or javascript? 使用 JavaScript 在表单内创建输入字段 - Creating input fields inside a form using JavaScript 如何使用javascript将表单值传递给表单中的其他字段 - How do I pass form values to other fields in a form with javascript 如何在可重复字段内动态求和输入值? - How do I dynamically sum input values inside a repeatable fields? 如何防止用户使用检查器工具更改表单值或删除输入字段元素? - How do I prevent users from changing form values or removing input fields elements using inspector tool? 如何修改我的代码以从弹出窗口收集表单输入,然后使用JavaScript将其插入到原始页面上光标位置的textarea中? - How do I modify my code to collect form input from a popup and insert it into a textarea at cursor location on original page using JavaScript? 如何使用javascript更新隐藏输入字段的值 - How do I use javascript to update the values of hidden input fields 如何在JavaScript中从多个表单输入值中找到数字总和? - How do I find the sum of numbers from multiple form input values in JavaScript? 如何从单个表单元素中的多个字段集中获取输入字段(包括选择,文本区域)的所有值? - How do I get all the value of Input fields (including select, text-area) from multiple fieldsets inside a single form element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM