Jquery - find the next input:text element after a known element

后端 未结 5 897
青春惊慌失措
青春惊慌失措 2020-12-11 05:54

I have an id for a Given the above, is it possible to find the next input:text element regardless of any other mark up in

5条回答
  •  渐次进展
    2020-12-11 06:42

    @Cawas - nice solution, but I suggest to use it not excessive, because of $('*') is expensive ;-)

    Regardless of that, I extended it for my needs:

    (function( $ ){
         $.fn.findNextOverall = function(sel, returnItselfIfMatched) {
    
           if(returnItselfIfMatched && $(this).is(sel)) return $(this);
    
           var $result = $(sel).first();
           if ($result.length <= 0) {
             return $result;
           }
           $result = [];
           var thisIndex = $('*').index($(this));
           var selIndex = Number.MAX_SAFE_INTEGER;
           $(sel).each(function(i,val){
             var valIndex = $('*').index($(val));
             if (thisIndex < valIndex && valIndex < selIndex) {
               selIndex = valIndex;
               $result = $(val);
             }
           });
           return $result;
         }; 
        })( jQuery );
    

    So if returnItselfIfMatched is set true it returns itself, if it matches the selector itself. Useful, if you don't know, which element is calling and you are searching for exactly itself.

提交回复
热议问题