我想获取所有输入并从我的页面HTML中选择元素。我已经尝试了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="">
答案 0 :(得分:0)
您应该分别访问元素; INPUT
和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元素及其属性。另外,请阅读纯JavaScript。
注意:尚未测试过。尝试一下,让我知道它是否符合您的要求。