Jquery find nearest matching element

筅森魡賤 提交于 2019-11-30 12:57:16

问题


I have a series of rows with columns and I want to select the value of an input field that is in a previous column to the input field (price input) that I am calling a function on when a key is released.

I have tried:

quantity = $(this).parent().parent().children().val() ;
quantity = $(this).parent().parent().children().closest('.inputQty', this).val() ;

But neither work.

An example of the DOM:

<div class="row">
    <div class="column"><input class="inputQty" id="quantity0" /></div>
    <div class="column"><input class="someOther" id="Other0" /></div>
    <div class="column">
        <div class="cSelect">
            <select id="currency0"><option>£</option></select>
            <input class="price" id="price0" />
        </div>
    </div>
</div>

回答1:


var otherInput = $(this).closest('.row').find('.inputQty');

That goes up to a row level, then back down to .inputQty.




回答2:


closest() only looks for parents, I'm guessing what you really want is .find()

$(this).closest('.row').children('.column').find('.inputQty').val();



回答3:


Get the .column parent of the this element, get its previous sibling, then find any input there:

$(this).closest(".column").prev().find("input:first").val();

Demo: http://jsfiddle.net/aWhtP/




回答4:


You could try:

$(this).closest(".column").prev().find(".inputQty").val();



来源:https://stackoverflow.com/questions/11756529/jquery-find-nearest-matching-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!