getElementsByTagName 多个标签
getElementsByTagName multiple tags
我想从我的页面 HTML 中获取所有输入和 select 元素。我已经尝试 getElementsByTagName('input,select')
但它不起作用。
我的代码 HTML 和 JavaScript:
function myFunction() {
var data = [];
var data1 = [];
var data2 = [];
for (var i = 0; i < 10; ++i) {
var x = document.getElementsByTagName("INPUT,SELECT")[i].getAttribute("name");
var y = document.getElementsByTagName("INPUT,SELECT")[i].getAttribute("type");
var z = document.getElementsByTagName("INPUT,SELECT")[i].getAttribute("maxlength");
data.push(x);
data1.push(y);
data2.push(z);
location.href ="ttt.php?name=" + data + "&active=" + data1 + "&data2=" + data2
}
}
<select name="CIV"><option selected="selected" value=""></option><option selected="selected" value="">Mr</option><option selected="selected" value="">Mme</option></select>
<input type="text" size="20" name="first_name" id="first_name" maxlength="50" class="cust_form" value="">
<input type="text" size="20" name="last_name" id="last_name" maxlength="50" class="cust_form" value="">
<input type="text" size="20" name="address3" id="address3" maxlength="50" class="cust_form" value="">
您应该单独访问这些元素; INPUT
和 SELECT
。顺便说一句,Select 元素没有 type
和 maxlength
属性。
function myFunction() {
var nameData = [];
var typeData = [];
var maxLengthData = [];
var input = document.getElementsByTagName("INPUT")
var select = document.getElementsByTagName("SELECT")
// Targeting the first 9 of elements of input collections and select collections
// Assuming you understand what you are doing here
for (var i = 0; i < 10; ++i) {
nameData.push( input[i].getAttribute( 'name' ) );
nameData.push( select[i].getAttribute( 'name' ) );
typeData.push( input[i].getAttribute( 'type' ) ); // Select element doesn't have type attribute
maxLengthData.push( inputt[i].getAttribute( 'maxlength' ) ); // Select elemnt doesn't have maxlength attribute
// Assuming you understand what you are doing to build your url here
location.href ="ttt.php?name=" + nameData + "&active=" + typeData + "&data2=" + maxLengthData
}
}
请详细阅读 HTML 元素及其属性。另外,阅读更多关于 pure JavaScript.
注意:这还没有经过测试。试一试,让我知道它是否能满足您的要求。
我想从我的页面 HTML 中获取所有输入和 select 元素。我已经尝试 getElementsByTagName('input,select')
但它不起作用。
我的代码 HTML 和 JavaScript:
function myFunction() {
var data = [];
var data1 = [];
var data2 = [];
for (var i = 0; i < 10; ++i) {
var x = document.getElementsByTagName("INPUT,SELECT")[i].getAttribute("name");
var y = document.getElementsByTagName("INPUT,SELECT")[i].getAttribute("type");
var z = document.getElementsByTagName("INPUT,SELECT")[i].getAttribute("maxlength");
data.push(x);
data1.push(y);
data2.push(z);
location.href ="ttt.php?name=" + data + "&active=" + data1 + "&data2=" + data2
}
}
<select name="CIV"><option selected="selected" value=""></option><option selected="selected" value="">Mr</option><option selected="selected" value="">Mme</option></select>
<input type="text" size="20" name="first_name" id="first_name" maxlength="50" class="cust_form" value="">
<input type="text" size="20" name="last_name" id="last_name" maxlength="50" class="cust_form" value="">
<input type="text" size="20" name="address3" id="address3" maxlength="50" class="cust_form" value="">
您应该单独访问这些元素; INPUT
和 SELECT
。顺便说一句,Select 元素没有 type
和 maxlength
属性。
function myFunction() {
var nameData = [];
var typeData = [];
var maxLengthData = [];
var input = document.getElementsByTagName("INPUT")
var select = document.getElementsByTagName("SELECT")
// Targeting the first 9 of elements of input collections and select collections
// Assuming you understand what you are doing here
for (var i = 0; i < 10; ++i) {
nameData.push( input[i].getAttribute( 'name' ) );
nameData.push( select[i].getAttribute( 'name' ) );
typeData.push( input[i].getAttribute( 'type' ) ); // Select element doesn't have type attribute
maxLengthData.push( inputt[i].getAttribute( 'maxlength' ) ); // Select elemnt doesn't have maxlength attribute
// Assuming you understand what you are doing to build your url here
location.href ="ttt.php?name=" + nameData + "&active=" + typeData + "&data2=" + maxLengthData
}
}
请详细阅读 HTML 元素及其属性。另外,阅读更多关于 pure JavaScript.
注意:这还没有经过测试。试一试,让我知道它是否能满足您的要求。