2011-09-26 73 views
4

我正在获取已获得值的表单的最后一个元素,现在我想要查找下一个div中的下一个输入元素的id ...找到下一个div-jquery中的下一个输入字段

var elem1 =$(':text[name^=distanceSlab][value!=""]').last(); 
var nextElemId = // find the id of next input element which is in next div 

相应的HTML代码

<div id ="divid4"> 
    <input type="text" id ="slab4" value ="1"/> // this is the last elemnt which has value 
</div> 

<div id ="div1d5"> 
     <input type="text" id ="slab5" value =""/> // need to find the id of this element 
</id> 

回答

8

假设你从你已经确定的input开始,然后:

$(this).closest('div').next('div').find('input:text').attr('id'); 

JS Fiddle

或者:

var thisIndex = $(this).index('input:text'); 

var next = thisIndex + 1; 

var nextElemId = $('input:text').eq(next).attr('id'); 

JS Fiddle

我可能会把这些放入blur()或类似的事件中。