[英]Run .each loop to get value of contents inside divs
I have an html structure similar to the one below 我有一个类似于下面的html结构
<div id="factsParent" class="col-md-12">
<div id="factBody1" style="margin-bottom:20px;" class="fact col-md-12 fact-body">
<div class="form-group col-md-10">
<div class="col-md-12">
<div class="box-wrap">
<input type="text" name="fact" placeholder="Enter a Fact" value="" class="fact form-control fact-title">
</div>
</div>
</div>
</div>
<div id="factBody2" style="margin-bottom:20px;" class="fact col-md-12 fact-body">
<div class="form-group col-md-10">
<div class="col-md-12">
<div class="box-wrap">
<input type="text" name="fact" placeholder="Enter a Fact" id="fact2" value="" class="fact form-control fact-title">
</div>
</div>
</div>
</div>
I would like to loop through each of the .fact classes and then get the value of each input type=text 我想遍历每个.fact类,然后获取每个输入type = text的值
$('.fact').each(function(i){
console.log(this.$('input[name=fact]').val());
});
Running the code above gave me an undefined error, how would I run a loop through all .facts and then get value of the input[type=text]; 运行上面的代码给了我一个未定义的错误,我将如何遍历所有.facts的循环,然后获取input [type = text]的值; I need to be able to get it exactly in this manner as I would be adding more input fields inside each div.
我需要能够以这种方式准确地获取它,因为我将在每个div中添加更多输入字段。
input elements has class .fact
. 输入元素具有类
.fact
。 you can simple iterate over input elements with class .fact
by modifying the selector: 您可以通过修改选择器来简单地对
.fact
类的输入元素进行迭代:
$('input.fact').each(function(i){
console.log($(this).val());//or this.value
});
for iterating over div elements: 用于遍历div元素:
$('div.fact').each(function(i){
console.log($(this).find('input').val());
});
The class .fact
is quite overused in your markup; .fact
类在您的标记中使用.fact
; it is not clear whether you're referring to div.fact
or input.fact
. 目前尚不清楚您是指
div.fact
还是input.fact
。 And there's another class that might cause confusion: input.Fact
. 还有另一个可能引起混淆的类:
input.Fact
。 So I have provided one of the possible ways you can approach this: 因此,我提供了一种可能的解决方法:
$('div.fact').each(function() {
console.log( $(':text', this).val() );
});
Of course , this code snippet is useless without an event. 当然 ,没有事件,此代码段是无用的。 For example you may have a button which after the user has provided input the user would then click the button to fire up this code snippet as follows:
例如,您可能有一个按钮,在用户提供输入后,用户将单击该按钮以触发此代码片段,如下所示:
$('button').on('click', function() {
//above code
});
$('div.fact').each(function() { console.log( $(':text', this).val() ); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="factsParent" class="col-md-12"> <div id="factBody1" style="margin-bottom:20px;" class="fact col-md-12 fact-body"> <div class="form-group col-md-10"> <div class="col-md-12"> <div class="box-wrap"> <input type="text" name="fact" placeholder="Enter a Fact" value="Test 1" class="fact form-control fact-title"> </div> </div> </div> </div> <div id="factBody2" style="margin-bottom:20px;" class="fact col-md-12 fact-body"> <div class="form-group col-md-10"> <div class="col-md-12"> <div class="box-wrap"> <input type="text" name="fact" placeholder="Enter a Fact" id="fact2" value="Test 2" class="fact form-control fact-title"> </div> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.