admin管理员组文章数量:1287652
I have a form with 3 input box and all the input box does not have id, name field in it. So if i enter value in it, How can i check the value of input box without id and name field using javascript
<form id='a' action='' >
<input type='text' value='' />
<input type='text' value='' />
</form>
This is the code in html and i want to have the value of input box using javascript. Can i do that?
I have a form with 3 input box and all the input box does not have id, name field in it. So if i enter value in it, How can i check the value of input box without id and name field using javascript
<form id='a' action='' >
<input type='text' value='' />
<input type='text' value='' />
</form>
This is the code in html and i want to have the value of input box using javascript. Can i do that?
Share Improve this question edited Oct 16, 2012 at 11:49 sv_in 14k9 gold badges37 silver badges55 bronze badges asked Oct 16, 2012 at 11:45 vishalgvishalg 4372 gold badges7 silver badges18 bronze badges 05 Answers
Reset to default 2You could get a reference to them and check their value
property.
For the luxury of supporting newer browsers...
[].forEach.call(document.querySelectorAll("#a input[type='text']"),
function(input) {
var value = input.value;
});
If you need to support the annoying browsers that still seem to linger, just write a bit more code and you're good as gold.
var inputs = document.getElementById("a").getElementsByTagName("input");
var i;
var length;
var value;
for (i = 0, length = inputs.length; i < length; i++) {
// Check we have [type='text']
if (inputs[i].type != "text") {
continue;
}
value = inputs[i].value;
}
you can get values by getElementsByTagName
and code would be like this
var inputs = document.getElementsByTagName('input');
value1 = inputs[0].value;
value2 = inputs[1].value;
You could use the elements
property of the form object which will iterate over just the input
elements inside the form:
for (var i = 0; i < a.length; i++) {
var e = a[i];
if (e.type == 'text') {
f(e.value);
}
}
var inpObj = document.getElementsByTagName('input');
for(var i in inpObj){
if(inpObj[i].type == "text"){
alert(inpObj[i].value);
}
}
This code will alert all the input textfields.
It looks like you want to do form validation. For form validation in HTML5, check this resource out: http://www.the-art-of-web./html/html5-form-validation/
Basically, you will be able to get by with some validations just by using HTML attributes, and without using JavaScript.
本文标签: htmlhow to get value of input box without id and name field using javascriptStack Overflow
版权声明:本文标题:html - how to get value of input box without id and name field using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741315661a2371897.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论