简体   繁体   English

如何 select div 不是 jQuery 中 div 的父级或兄弟级

[英]How to select div which is not parent nor sibling of a div in jQuery

I have many divs like this我有很多这样的div

        <div class="body-row row m-0">
            <div class="px-2 col-3">
                <select name="item_type[]" class="form-control invoiceType" required>
                    <option value="newitem">New Item</option>
                    <option value="presetitem">Present Item</option>
                    <option value="discount">Discount</option>
                    <option value="subsidy">Subsidy</option>
                </select>
            </div>
            <div class="px-2 col-6 item-description" style="display: flex;">
                <input type="text" class="form-control" name="description[]" placeholder="Add Invoice Description" required style="flex: 1">
            </div>
            <div class="px-2 col-2">
                <input type="number" class="form-control" name="amount[]" placeholder="0" required>
            </div>
            <div class="col-1 del-icon-container"><i class="fas fa-trash del-invoice-item"></i></div>
        </div>

I want to select only item-description , and remove the content in it我想 select 只有item-description ,并删除其中的内容

$(document).on('change', '.invoiceType',function() {
        var invoiceType = $(this).find(":selected").val();
        if(invoiceType == 'newitem') {
            $('.item-description').empty();
            $('.item-description').append(`
                <input type="text" class="form-control" name="description[]" placeholder="Add Invoice Description" required style="flex: 1">
            `);
        } 
    });

Currently it make changes to all divs because they have all same name of classes目前它对所有 div 进行更改,因为它们具有所有相同的类名称

You can use jQuery's DOM traversal methods to relate elements to each other based on a reference to an element in an event handler.您可以使用 jQuery 的 DOM 遍历方法基于对事件处理程序中元素的引用将元素相互关联。 In this case you can use closest() to find a common parent container, then find() to get the element you require.在这种情况下,您可以使用closest()来查找公共父容器,然后使用find()来获取您需要的元素。

Note in the example below that the :selected selector is not necessary - you can get the val() from the select directly.请注意,在下面的示例中, :selected选择器不是必需的 - 您可以直接从select获取val() In addition you can combine empty() and append() by setting the html() of the element.此外,您可以通过设置元素的html()来组合empty()append()

 $(document).on('change', '.invoiceType', function() { console.log('change'); let $field = $(this); let invoiceType = $field.val(); //:selected not needed if (invoiceType === 'newitem') { $field.closest('.body-row').find('.item-description').html(`<input type="text" class="form-control" name="description[]" placeholder="Add Invoice Description" required style="flex: 1">`); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="body-row row m-0"> <div class="px-2 col-3"> <select name="item_type[]" class="form-control invoiceType" required> <option value="newitem">New Item</option> <option value="presetitem">Present Item</option> <option value="discount">Discount</option> <option value="subsidy">Subsidy</option> </select> </div> <div class="px-2 col-6 item-description" style="display: flex;"> <input type="text" class="form-control" name="description[]" placeholder="Add Invoice Description" required style="flex: 1"> </div> <div class="px-2 col-2"> <input type="number" class="form-control" name="amount[]" placeholder="0" required> </div> <div class="col-1 del-icon-container"><i class="fas fa-trash del-invoice-item"></i></div> </div> <div class="body-row row m-0"> <div class="px-2 col-3"> <select name="item_type[]" class="form-control invoiceType" required> <option value="newitem">New Item</option> <option value="presetitem">Present Item</option> <option value="discount">Discount</option> <option value="subsidy">Subsidy</option> </select> </div> <div class="px-2 col-6 item-description" style="display: flex;"> <input type="text" class="form-control" name="description[]" placeholder="Add Invoice Description" required style="flex: 1"> </div> <div class="px-2 col-2"> <input type="number" class="form-control" name="amount[]" placeholder="0" required> </div> <div class="col-1 del-icon-container"><i class="fas fa-trash del-invoice-item"></i></div> </div>

Finally, as an aside, note that creating an entirely new input and overwriting the content of the div that contains it is a little redundant.最后,顺便说一句,请注意创建一个全新的input并覆盖包含它的 div 的内容有点多余。 Simply calling val('') on the input will suffice is all you need to do is clear its value:只需在输入上调用val('')就足够了,您需要做的就是清除它的值:

$field.closest('.body-row').find('.item-description input').val('');

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

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