简体   繁体   English

从html表中获取所有文本字段数据

[英]get All the text fields data from the html table

Get All the data values from the auto generated table and show these values through loop to user by JavaScript alert.I have implemented but it is not showing anything in alert.how i can take the values from these autogenrated text fields. 从自动生成的表中获取所有数据值,并通过JavaScript alert通过循环向用户显示这些值。我已经实现了,但是在alert.in中什么也没有显示。我如何从这些自动生成的文本字段中获取值。

 function myFunction() { var table = document.getElementById("myTable"); var table1 = document.getElementById("myTable").length; var row = table.insertRow(0); var cell1 = row.insertCell(0); var cell2 = row.insertCell(1); var cell3 = row.insertCell(2); var textf1 = '<div>FirstName:<input type="text" value="Enter Your Name" id="text1" /></div>'; var textf2 = '<div>LastName:<input type="text" value="Enter Your Surname" id="text2" /></div>'; cell1.innerHTML = textf1; cell2.innerHTML = textf2; } function first(){ var x = document.getElementById("myTable"); var textn = ""; var texts= ""; var i; var a; for (i = 0; i < x.length ;i++) { textn += text1[i].value + "<br>"; } for (a = 0; i < x.length ;i++) { texts += text2[i].value + "<br>"; } alert("First Name:"+textn"Second Name:"+texts); /*var table1 = document.getElementById("myTable").length; for(var row = 0; row <= table1;row++ ) { alert("Hello" + text1.value(i) + "Your Surname Is " + text2.value(i) + " You Have Chosen"); } /*return myFunction()*/ } 
 <!DOCTYPE html> <html> <head> <script type="text/javascript" src="js/myweb.js"> </script> </head> <body> <p>Click the button to add a new row at the first position of the table and then add cells and content.</p> <table id="myTable"></table> <table id="myTable1"></table> <br> <div id="first"></div> <button onclick="myFunction()">Add Your First row</button> <button onclick="first()">Submit</button> </body> </html> 

function first(){
        var x = document.getElementById("myTable");
        var inputElements = x.querySelectorAll('input, select, textarea');
        for(var i = 0; i < inputElements.length; i++){

             alert(inputElements[i].value);
        }

    }
alert("First Name:"+textn"Second Name:"+texts);

缺少+

alert("First Name:"+textn+"Second Name:"+texts);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM