如何获取第n个div';使用jquery或javascript创建子元素值
我想得到第四个div的子输入元素的值。 基本上,我正在检查chekbox值,并希望使用“this”关键字添加相关的输入框值 这是我的html代码如何获取第n个div';使用jquery或javascript创建子元素值,javascript,jquery,Javascript,Jquery,我想得到第四个div的子输入元素的值。 基本上,我正在检查chekbox值,并希望使用“this”关键字添加相关的输入框值 这是我的html代码 <div class="container"> <div class="cell-checkbox" style="float:left;margin-right:5%;"> <input type="checkbox" name="select" value="7" class="big">
<div class="container">
<div class="cell-checkbox" style="float:left;margin-right:5%;">
<input type="checkbox" name="select" value="7" class="big">
</div>
<div class="cell-desc" style="float:left;margin-right:5%;width:30%;">
<!-- Content -->
</div>
<div class="cell-cart"> //input box is inside this div
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td align="right">
<table align="center" class="cellBuy">
<tbody>
<tr align="right">
<td width="1%">
<input type="hidden" name="buyid" id="buyid" value="7">
<input type="hidden" name="category" value="464">
<input type="hidden" name="itemid" value="">
<input name="qty" id="qty" size="6" maxlength="6" value="1" class="input"> /*want to get this input text value
</td>
<td height="20">
<div align="left">
<input type="button" class="btn-BuyOff" value="Buy" id="addtocart" name="addtocart">
</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</div>
您对dom遍历有问题
$(this).parent()
不要返回包含输入复选框元素的div。您应该使用类容器遍历到最近的div,然后在其中找到输入复选框。像这样:
var result = [];
$(':checkbox:checked').each(function (i) {
result.push($(this).val() + "," + $(this).closest('.container').find('[name="qty"]').val());// this is for finding quantity field value
});
var strreuslt = result.join(';');
alert(strreuslt);
您对dom遍历有问题
$(this).parent()
不要返回包含输入复选框元素的div。您应该使用类容器遍历到最近的div,然后在其中找到输入复选框。像这样:
var result = [];
$(':checkbox:checked').each(function (i) {
result.push($(this).val() + "," + $(this).closest('.container').find('[name="qty"]').val());// this is for finding quantity field value
});
var strreuslt = result.join(';');
alert(strreuslt);
$(':checkbox')。等式(3)
这将为您提供jquery中的第四个元素。索引从零开始,因此.eq(3)将给出第四个子项。$(':checkbox')。eq(3)
这将为您提供jquery中的第四个元素。索引从零开始,因此.eq(3)将给出第四个子元素。使用
名称
,不要对多个元素使用相同的id
:
$(this).parent().siblings('.cell-cart').find('[name=qty]').val();
如果容器包含多个
,则使用此选项:
请改用
名称
,切勿对多个元素使用相同的id
:
$(this).parent().siblings('.cell-cart').find('[name=qty]').val();
如果容器包含多个
,则使用此选项:
尝试使用“最近”获取包含两个输入的最近父级,然后触发查找
$(':checkbox:checked').each(function (i) {
result.push($(this).val() + "," + $(this).closest('.container').find('input[name="qty"]').val());
});
或:
注意:如果有多组输入,则需要将每个组包装在dom元素中,最好是
ul li
尝试使用“最近”来获取包含这两个输入的最近父级,然后触发查找
$(':checkbox:checked').each(function (i) {
result.push($(this).val() + "," + $(this).closest('.container').find('input[name="qty"]').val());
});
或:
注意:如果有多组输入,则需要将每个组封装在dom元素中,最好是
ulli
我希望获得类似$(this).parent().next().find('#qty').val()这样的值#qty是Id,对于整个页面来说是唯一的,所以您不需要遍历复杂的查询,而是通过$('#qty')得到结果。val()@MantasČekanauskas.eq(4)将给出第五个子项,而不是第四个子项。第四个孩子将由等式(3)给出。索引从0开始。@Yogita088您完全正确,从零开始的索引有时会令人困惑;)我想得到类似于$(this.parent().next().find('#qty').val()的值#qty是Id,对于整个页面来说是唯一的,所以您不需要遍历复杂的查询,而是通过$('#qty')得到结果。val()@MantasČekanauskas.eq(4)将给出第五个子项,而不是第四个子项。第四个孩子将由等式(3)给出。索引从0开始。@Yogita088您完全正确,从零开始的索引有时会令人困惑;)我想得到类似于$(this.parent().next().find('#qty').val()的值;我想得到类似于$(this.parent().next().find('#qty').val()的值;是否有多个元素具有id=“qty”
?是否有多个元素具有id=“qty”
?