2010-11-07 98 views
1
$("input[name='add'].my").click(function(){ 
    //i tried use this <input type="button" name="add" value=..../> 
    //element to find his parent's parent the `<div>` object 
    //how can i get it 

}); 


<div> 
    <tr> 
    <td><%= post.title %></td> 
    <td><%= post.content %></td> 
    <td><%= link_to 'Show', post %></td> 
    <td><%= link_to 'Edit', edit_post_path(post),:remote=>true %></td> 
    <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',:remote=>true, :method => :delete %></td> 
    <td><input type="button" name="add" value="add" class="my"/></td> 
    <td></td> 
    </tr> 
</div> 
<div> 
    <tr> 
    <td><%= post.title %></td> 
    <td><%= post.content %></td> 
    <td><%= link_to 'Show', post %></td> 
    <td><%= link_to 'Edit', edit_post_path(post),:remote=>true %></td> 
    <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',:remote=>true, :method => :delete %></td> 
    <td><input type="button" name="add" value="add" class="my"/></td> 
    <td></td> 
    </tr> 
</div> 

回答

1

在這裏你去:

$(function() { 
    $("input[name='add'] .my").click(function(){ 
     var parent = $(this).parent().parent().parent(); 
     //OR 
     var parent = $(this).closest('div'); 
     //do stuff... 
    }); 
}); 
3

這將讓你最接近的父DIV:

$("input[name='add'].my").click(function(){ 
    var parentDiv = jQuery(this).closest("div"); 
});