<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="button" id="btn" value="获取文本框的值">
var textInputs = document.getElementsByTagName('input');
setValue(textInputs);
function setValue(element) {
for (var i = 0; i < element.length; i++) {
if (element[i].type === 'text') {
element[i].value = i;
}
}
}
var btn = document.getElementById('btn');
var arr = [];
btn.onclick = function () {
for (var i = 0; i < textInputs.length; i++) {
if (textInputs[i].type === 'text') {
arr.push(textInputs[i].value);
}
}
arr = arr.join('|');
console.log(arr);
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5940e225c122d09d8ec5aed4b4127a5c.jpeg)