[英]how to get child of div element
<div class="div" id="div">
<div class="row">
<div class="col1">
<input type="text">
<!--I need to get a value of these inputs-->
</div>
<div class="col2">
<input type="text">
<!--I need to get a value of these inputs-->
</div>
</div>
<div class="row">
<div class="col1">
<input type="text">
<!--I need to get a value of these inputs-->
</div>
<div class="col2">
<input type="text">
<!--I need to get a value of these inputs-->
</div>
</div>
</div>
How to get values of all of inputs in div and how to check in which div input in(col1 or col2); 如何获取div中所有输入的值以及如何在in(col1或col2)中检查哪个div输入;
in my case div with class "row" will be added via firebase database. 在我的情况下,类“行”的div将通过firebase数据库添加。
const inputs = document.querySelectorAll(".div > .col");
alert(inputs.value);
Using JavaScript, try looping with forEach
使用JavaScript,尝试使用forEach
循环
var inputElem = document.querySelectorAll(".row input");
inputElem.forEach(function(obj){
console.log(obj.value)
})
Snippet: 片段:
var inputElem = document.querySelectorAll(".row input"); inputElem.forEach(function(obj){ console.log(obj.value) })
<div class="div" id="div"> <div class="row"> <div class="col1"> <input type="text" value="one"> <!--I need to get a value of these inputs--> </div> <div class="col2"> <input type="text" value="two"> <!--I need to get a value of these inputs--> </div> </div> <div class="row"> <div class="col1"> <input type="text" value="three"> <!--I need to get a value of these inputs--> </div> <div class="col2"> <input type="text" value="four"> <!--I need to get a value of these inputs--> </div> </div> </div>
I think this could work: 我认为这可能有效:
$("div input").each(function(){
// Get the value:
console.log($(this).val());
// check in which div input in(col1 or col2)
console.log($(this).closest("div").attr("class"));
});
Try this: 尝试这个:
const inputs = document.getElementById('div').getElementsByTagName('input');
console.log(inputs[0].value, inputs[1].value);
See an example here: https://jsfiddle.net/xbdd6q13/ 在此处查看示例: https : //jsfiddle.net/xbdd6q13/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.