簡體   English   中英

jQuery最近獲取父Div元素的屬性

[英]Jquery Closest getting Attributes of Elements of Parent Div

我有一個重復的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元素

嘗試使用.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);
     });
});

parent是目標元素的類值,因此您需要將其與.parent等類選擇器.parent

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

嘗試這個,

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

演示版

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM