2013-11-01 116 views
0

我jQuery中有一个重复的div结构的Jquery最近获得的属性

<div class="parent"> 
    <input type="text" name="name" class="name" value="test"> 
    <input type="hidden" name="id" class="id" value="123"> 
    <span class="btns"> 
    <button name="btn_clr" class="clear" type="button" value="Clear"></button> 
    </span> 
    </div> 

我有明确的按钮

$('.clear').bind("click", function (e){ 
    $(this).closest('.parent').find('input').each(function (index){      
     console.log(this); 
    }); 
}); 

我想清除出值的onclick功能的Paret Class DIV的输入元素,但它没有获得每个函数中的INPUT元素

回答

0

试试这个,

$('.clear').bind("click", function (e){ 
    $(this).closest('.parent').find('input').each(function (index){ 
     $(this).val(''); // to clear the value 
    }); 
}); 

Demo

0

尝试使用.parent()

$('.clear').bind("click", function (e){ 
    $(this).closest('.btns').parent().find('input').each(function (index){      
      console.log(this); 
    }); 
}); 

甚至尝试像

$('.clear').bind("click", function (e){ 
    $(this).closest('.parent').find('input').each(function (index){      
      console.log(this); 
    }); 
}); 
0

parent是目标元素的类值,所以你需要有一个类选择使用它像.parent

$('.clear').bind("click", function (e) { 
    $(this).closest('.parent').find('input').each(function (index) { 
     console.log(this); 
    }); 
});