繁体   English   中英

如何使用jQuery或JavaScript获取第n个div的子元素值

[英]How to get nth div's child element value using jquery or javascript

我想获取第4个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>
    <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
                                        &nbsp;
                                    </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>  

我已经尝试了下面提到的代码,但是它不起作用

var result = [];
$(':checkbox:checked').each(function (i) {
    result.push($(this).val() + "," + $(this).parent().next().find('#qty').val());// this is for finding quantity field value
});
var strreuslt = result.join(';');
alert(strreuslt);

$(':checkbox').eq(3)这在jquery中为您提供了第4个元素。 索引从零开始,因此.eq(3)将给出第四个孩子。

尝试使用最接近来获取包含两个输入的最接近父级,然后触发查找

$(':checkbox:checked').each(function (i) {
    result.push($(this).val() + "," + $(this).closest('.container').find('input[name="qty"]').val());
});

要么:

  $(':checkbox:checked').each(function (i) {
        result.push($(this).val() + "," + $(this).parent().siblings('.cell-cart').find('input[name="qty"]').val());
    });

注意:如果您有多组输入,则需要将每个组包装在一个dom元素中,最好是ul li

您在遍历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);

请改用name ,并且永远不要对多个元素使用相同的id

$(this).parent().siblings('.cell-cart').find('[name=qty]').val();

如果容器包含多个<div class="cell-cart">则使用此方法:

$(this).parent().next().next().find('[name=qty]').val();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM