我想迭代一个元素数组,并查询每个
的子元素到目前为止,我已经尝试了
$.each($("#new_shipment .package_dimensions .package_dimension"), function() {
var amount = $(this).children('observe.amount').val()
var weight = $(this).children('observe.weight').val()
console.log(amount);
console.log(weight);
HTML"
<h2>Packages</h2>
<div class='packages form_pane'>
<div class='package_dimensions'>
<div class='dimensions_text'>
<p>Dimensions in cm/kg</p>
</div>
<div class='labels'>
<label>Amount</label>
<label>Length</label>
<label>Width</label>
<label>Height</label>
<label>Weight</label>
</div>
<div class='dimensions'>
<div class='package_dimension' data-row-id='0' id='package_dimension_0'>
<div class="input string required shipment_package_dimensions_amount"><input class="string required observe amount" id="shipment_package_dimensions_0_amount" name="shipment[package_dimensions][0][amount]" type="text" /></div>
<div class='math_label'>×</div>
<div class="input string required shipment_package_dimensions_length"><input class="string required observe length" id="shipment_package_dimensions_0_length" name="shipment[package_dimensions][0][length]" type="text" value="15" /></div>
<div class='math_label'>×</div>
<div class="input string required shipment_package_dimensions_width"><input class="string required observe width" id="shipment_package_dimensions_0_width" name="shipment[package_dimensions][0][width]" type="text" value="12" /></div>
<div class='math_label'>×</div>
<div class="input string required shipment_package_dimensions_height"><input class="string required observe height" id="shipment_package_dimensions_0_height" name="shipment[package_dimensions][0][height]" type="text" value="10" /></div>
<div class='math_label'> </div>
<div class="input string required shipment_package_dimensions_weight"><input class="string required observe_weight" id="shipment_package_dimensions_0_weight" name="shipment[package_dimensions][0][weight]" type="text" value="10.0" /></div>
</div>
......
</div>
然而,当我记录数量和重量的结果时,我得到了未定义的
有什么想法吗?
答案 0 :(得分:1)
在.
中使用课程observe
时,您缺少班级选择器(children()
)。您还应该使用.find()
而不是.children()
,因为输入元素不是所选div的直接子元素:
$.each($("#new_shipment .package_dimensions .package_dimension"), function() {
var amount = $(this).find('.observe.amount').val()
var weight = $(this).find('.observe_weight').val()
console.log(amount);
console.log(weight);
});