Best way to find “next” form input element in jQuery?

前端 未结 12 1633
滥情空心
滥情空心 2020-12-02 18:18

Using jQuery, what\'s the best way to find the next form element on the page, starting from an arbitrary element? When I say form element I mean ,

12条回答
  •  余生分开走
    2020-12-02 19:14

    You can do this to take a complete list of the form elements you are looking for:

    var yourFormFields = $("yourForm").find('button,input,textarea,select');
    

    Then, should be easy find the next element:

    var index = yourFormFields.index( this ); // the index of your current element in the list. if the current element is not in the list, index = -1
    
    if ( index > -1 && ( index + 1 ) < yourFormFields.length ) { 
    
                        var nextElement = yourFormFields.eq( index + 1 );
    
                        }
    

提交回复
热议问题