2013-08-05 56 views
0

我有几个参数传递给一个js函数(我使用jeditable)。传递最接近的元素的ID作为参数值jquery

$('.edit_area').editable('update.php',{ 
id  : '2', 
type  : 'textarea' 
}); 

.edit_area是<p>元件。我需要将'id'的值作为最接近父元素<div>的值 传递。

任何帮助,将不胜感激。谢谢!

回答

1

如果你只有一个.edit_area元素在页面

$('.edit_area').editable('update.php', { 
    id: $('.edit_area').closest('div').att('id'), 
    type: 'textarea' 
}); 

如果你有多个元素

$('.edit_area').each(function(){ 
    var $this = $(this); 
    $this.editable('update.php', { 
     id: $this..closest('div').att('id'), 
     type: 'textarea' 
    }); 
}) 
+0

有几个错别字,但工作! –

0

尝试

$('#2').parents('div:eq(0)').attr('id'); //where 2 is your param 
1

使用closest()拿到第一个div在p。的层级中arents。

$('.edit_area').editable('update.php',{ 
    id : $('.edit_area').closest('div').attr('id'), 
    type : 'textarea' 
}); 

closest()

对于组中的每一个元素,获得通过自身测试 元素和DOM树向上遍历通过它的祖先 选择相匹配的第一个元素,reference