Jquery最近得到父Div的元素的属性

时间:2013-11-01 10:40:01

标签: javascript jquery

我有一个重复的div结构

<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>

在Jquery中我有一个用于清除按钮的onclick函数

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

我想清除Paret Class DIV的输入元素的值,但是没有在每个函数中获取INPUT元素

3 个答案:

答案 0 :(得分: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);
     });
});

答案 1 :(得分:0)

parent是目标元素的类值,因此您需要将其与类.parent

之类的类选择器一起使用
$('.clear').bind("click", function (e) {
    $(this).closest('.parent').find('input').each(function (index) {
        console.log(this);
    });
});

答案 2 :(得分:0)

试试这个,

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

Demo