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

后端 未结 5 799
青春惊慌失措
青春惊慌失措 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:22

    You cannot have a div tag in between TR's. It is not a valid markup.

    To find the input element from the referred tr you can try this.

    $('#tagTR').nextAll().filter(function(){
        return $(this).find('input:text').length > 1;
    }).find('input');
    
    0 讨论(0)
  • 2020-12-11 06:32

    Check it out: moved this into a more complete question and answer

    Based on the awesome answer by @mrtsherman, I wrote this more complete solution:

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

    Then you can use it as this:

    $('#tagTR').findNext('input');
    

    I would submit a PR for this on jQuery lib if I knew my code was properly optimized and if it was okay by Mr T Sherman there, meaning I think this should be a core method on jQuery! :P

    0 讨论(0)
  • 2020-12-11 06:33

    I thought this question was very interesting. It seems others are reading this as, find the next input among siblings. But I read it as - find me the next input no matter what. I don't know if its in a sibling, a parent or a parent's sibling. This is what I came up with based on feedback I received from this question.

    http://jsfiddle.net/GesSj/1

    //assume you know where you are starting from
    var $startElement = $('#foo');
    
    //get all text inputs
    var $inputs = $('input[type=text]');
    
    //search inputs for one that comes after starting element
    for (var i = 0; i < $inputs.length; i++) {
        if (isAfter($inputs[i], $startElement)) {
            var nextInput = $inputs[i];
            alert($(nextInput).val());
        }
    }
    
    //is element before or after
    function isAfter(elA, elB) {
        return ($('*').index($(elA).last()) > $('*').index($(elB).first()));
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-11 06:45

    You can use "next":

    $("#tagTR").next("input")
    
    0 讨论(0)
提交回复
热议问题